You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
يبحث محرك التعبير العادي عن البدائل واحدة تلو الأخرى. هذا هو: أولاً يتحقق مما إذا كان لدينا `match:Java` ، وإلا - يبحث عن`match:JavaScript`وما إلى ذلك.
14
+
The regular expression engine looks for alternations one-by-one. That is: first it checks if we have `match:Java`, otherwise -- looks for `match:JavaScript`and so on.
15
15
16
-
ونتيجة لذلك ، لا يمكن العثور على `match:JavaScript`مطلقًا ، فقط لأنه يتم تحديد`match:Java`أولاً.
16
+
As a result, `match:JavaScript`can never be found, just because `match:Java`is checked first.
17
17
18
-
نفس الشيء مع`match:C`و`match: C ++ `.
18
+
The same with`match:C`and `match:C++`.
19
19
20
-
هناك حلان لهذه المشكلة:
20
+
There are two solutions for that problem:
21
21
22
-
1.قم بتغيير الترتيب للتحقق من المطابقة الأطول أولاً: `pattern:JavaScript | Java | C \ + \ + | C | PHP`.
23
-
2.دمج المتغيرات بنفس البداية: `pattern:Java(Script)؟ | C (\ + \ +)؟ | PHP`.
22
+
1.Change the order to check the longer match first: `pattern:JavaScript|Java|C\+\+|C|PHP`.
23
+
2.Merge variants with the same start: `pattern:Java(Script)?|C(\+\+)?|PHP`.
Copy file name to clipboardExpand all lines: 9-regular-expressions/13-regexp-alternation/02-find-matching-bbtags/solution.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
2
-
علامة الفتح هي `النمط: \ [(b | url | الاقتباس) \]`.
2
+
Opening tag is `pattern:\[(b|url|quote)\]`.
3
3
4
-
ثم للعثور على كل شيء حتى علامة الإغلاق - دعنا نستخدم النمط `pattern:. *؟` مع العلامة`pattern:s`لمطابقة أي حرف بما في ذلك السطر الجديد ثم إضافة مرجع خلفي إلى علامة الإغلاق.
4
+
Then to find everything till the closing tag -- let's use the pattern `pattern:.*?` with flag`pattern:s`to match any character including the newline and then add a backreference to the closing tag.
يرجى ملاحظة أنه بالإضافة إلى الهروب من `نمط: [`و `نقش:]` ، كان علينا أن نهرب من شرطة مائلة لنموذج علامة الإغلاق: [\ / \ 1] `، لأن الخط المائل يغلق النمط عادةً.
23
+
Please note that besides escaping `pattern:[`and `pattern:]`, we had to escape a slash for the closing tag `pattern:[\/\1]`, because normally the slash closes the pattern.
-ثم إذا كان لدينا نمط `` الشرطة المائلة للخلف '': `` (يجب علينا تقنيًا مضاعفته في النمط ، لأنه شخصية خاصة ، لذلك فهي خط مائل عكسي في الواقع) ، فإن أي حرف يكون جيدًا بعده (نقطة ).
7
-
-وإلا فإننا نأخذ أي حرف باستثناء الاقتباس (وهذا يعني نهاية السلسلة) وشرطة مائلة للخلف (لمنع الخطوط المائلة العكسية الوحيدة ، يتم استخدام الشرطة المائلة للخلف فقط مع بعض الرموز الأخرى بعدها): `النمط: [^"\\]`
8
-
- ... وهلم جرا حتى آخر quote أو علامة افتباس .
5
+
-First we look for an opening quote `pattern:"`
6
+
-Then if we have a backslash `pattern:\\` (we technically have to double it in the pattern, because it is a special character, so that's a single backslash in fact), then any character is fine after it (a dot).
7
+
-Otherwise we take any character except a quote (that would mean the end of the string) and a backslash (to prevent lonely backslashes, the backslash is only used with some other symbol after it): `pattern:[^"\\]`
8
+
- ...And so on till the closing quote.
9
9
10
-
بشكل:
10
+
In action:
11
11
12
12
```js run
13
13
let regexp =/"(\\.|[^"\\])*"/g;
14
14
let str =' .. "test me" .. "Say \\"Hello\\"!" .. "\\\\\\"" .. ';
قم بإنشاء regexp للعثور على سلاسل في علامات اقتباس مزدوجة `الموضوع:" ..."`.
3
+
Create a regexp to find strings in double quotes `subject:"..."`.
4
4
5
-
يجب أن تدعم السلاسل الهروب ، بنفس الطريقة التي تدعمها سلاسل JavaScript. على سبيل المثال ، يمكن إدراج علامات الاقتباس كـ `subject: \" `سطر جديد مثل`subject: \ n` ، والشرطة نفسها كـ `subject:\\`.
5
+
The strings should support escaping, the same way as JavaScript strings do. For instance, quotes can be inserted as `subject:\"` a newline as `subject:\n`, and the slash itself as `subject:\\`.
6
6
7
7
```js
8
8
let str ="Just like \"here\".";
9
9
```
10
10
11
-
يرجى ملاحظة ، على وجه الخصوص ، أن الاقتباس الهارب `موضوع: \" `لا ينهي سلسلة.
11
+
Please note, in particular, that an escaped quote `subject:\"` does not end a string.
12
12
13
-
لذلك يجب علينا البحث من اقتباس واحد إلى الآخر تجاهل علامات الاقتباس الهاربة على الطريق.
13
+
So we should search from one quote to the other ignoring escaped quotes on the way.
14
14
15
-
هذا هو الجزء الأساسي من المهمة ، وإلا سيكون تافها.
15
+
That's the essential part of the task, otherwise it would be trivial.
اكتب regexp للعثور على العلامة `<style...>`. يجب أن تتطابق مع العلامة الكاملة: قد لا تحتوي على سمات `<style>`أو تحتوي على العديد منها`<style type =" ... "id =" ...">`.
3
+
Write a regexp to find the tag `<style...>`. It should match the full tag: it may have no attributes `<style>`or have several of them`<style type="..." id="...">`.
لقد رأينا بالفعل شيئًا مشابهًا - الأقواس المربعة. تسمح بالاختيار بين عدة أحرف ، على سبيل المثال`pattern: gr [ae]y`يطابق`match: gre` أو`match:grey`.
21
+
We already saw a similar thing -- square brackets. They allow to choose between multiple characters, for instance`pattern:gr[ae]y`matches `match:gray` or`match:grey`.
22
22
23
-
تسمح الأقواس المربعة باستخدام الأحرف أو مجموعات الأحرف فقط. يسمح التناوب بأي تعبيرات. نمط regexp `: A | B | C`يعني أحد التعبيرات` A` أو `B`أو`C`.
23
+
Square brackets allow only characters or character sets. Alternation allows any expressions. A regexp `pattern:A|B|C`means one of expressions `A`, `B`or `C`.
24
24
25
-
على سبيل المثال:
25
+
For instance:
26
26
27
-
-`pattern: gr (a | e) y`يعني تمامًا مثل`pattern: gr [ae]y`.
28
-
-`pattern:gra | ey`تعني`match:gra`أو`match:ey`.
27
+
-`pattern:gr(a|e)y`means exactly the same as `pattern:gr[ae]y`.
28
+
-`pattern:gra|ey`means `match:gra`or`match:ey`.
29
29
30
-
لتطبيق التناوب على جزء مختار من النمط ، يمكننا تضمينه بين قوسين:
31
-
-`النمط: أحب HTML | CSS`يطابق` مطابقة: أنا أحب HTML`أو `تطابق: CSS`.
32
-
-`pattern:I love (HTML | CSS)`يتطابق مع `match:I love HTML`أو`match:I love CSS`.
30
+
To apply alternation to a chosen part of the pattern, we can enclose it in parentheses:
31
+
-`pattern:I love HTML|CSS`matches `match:I love HTML`or `match:CSS`.
32
+
-`pattern:I love (HTML|CSS)`matches `match:I love HTML`or `match:I love CSS`.
33
33
34
-
## مثال: regexp للوقت
34
+
## Example: regexp for time
35
35
36
-
في المقالات السابقة ، كانت هناك مهمة لبناء regexp لوقت البحث في شكل `hh:mm` ، على سبيل المثال`12:00`. لكن `` النمط البسيط: \ d \ d: \ d \ d`غامض للغاية. يقبل `25:99` كوقت (حيث تتطابق 99 ثانية مع النمط ، لكن ذلك الوقت غير صالح).
36
+
In previous articles there was a task to build a regexp for searching time in the form `hh:mm`, for instance `12:00`. But a simple `pattern:\d\d:\d\d`is too vague. It accepts `25:99`as the time (as 99 seconds match the pattern, but that time is invalid).
37
37
38
-
كيف يمكننا صنع نمط أفضل؟
38
+
How can we make a better pattern?
39
39
40
-
يمكننا استخدام مطابقة أكثر دقة. اولا الساعات:
40
+
We can use more careful matching. First, the hours:
41
41
42
-
-إذا كان الرقم الأول هو "0" أو "1" ، فيمكن أن يكون الرقم التالي أي: `pattern:[01] \ d`.
43
-
-وإلا ، إذا كان الرقم الأول هو "2" ، فيجب أن يكون الرقم التالي "pattern:[0-3]`.
44
-
- (غير مسموح بالرقم الأول الآخر)
42
+
-If the first digit is `0` or `1`, then the next digit can be any: `pattern:[01]\d`.
43
+
-Otherwise, if the first digit is `2`, then the next must be `pattern:[0-3]`.
44
+
- (no other first digit is allowed)
45
45
46
-
يمكننا كتابة كلا المتغيرين في regexp باستخدام البديل: `pattern:[01] \ d | 2 [0-3]`.
46
+
We can write both variants in a regexp using alternation: `pattern:[01]\d|2[0-3]`.
47
47
48
-
بعد ذلك ، يجب أن تكون الدقائق من `00`إلى`59`. في لغة التعبير العادي التي يمكن كتابتها كـ `pattern:[0-5] \ d`: الرقم الأول`0-5` ، ثم أي رقم.
48
+
Next, minutes must be from `00`to `59`. In the regular expression language that can be written as `pattern:[0-5]\d`: the first digit `0-5`, and then any digit.
49
49
50
-
إذا صقنا الدقائق والثواني معًا ، نحصل على النمط: `pattern:[01] \ d | 2 [0-3]:[0-5] \ d`.
50
+
If we glue minutes and seconds together, we get the pattern: `pattern:[01]\d|2[0-3]:[0-5]\d`.
51
51
52
-
لقد انتهينا تقريبًا ، ولكن هناك مشكلة. يحدث "النمط البديل": | `الآن بين` النمط: [01] \ d`و "النمط: 2 [0-3]:[0-5] \ d`.
52
+
We're almost done, but there's a problem. The alternation `pattern:|` now happens to be between `pattern:[01]\d`and `pattern:2[0-3]:[0-5]\d`.
53
53
54
-
أي: تمت إضافة الدقائق إلى البديل الثاني ، إليك صورة واضحة:
54
+
That is: minutes are added to the second alternation variant, here's a clear picture:
55
55
56
-
``
57
-
[01] \ d | 2 [0-3]:[0-5] \ د
58
-
``
56
+
```
57
+
[01]\d | 2[0-3]:[0-5]\d
58
+
```
59
59
60
-
يبحث هذا النمط عن `النمط: [01] \ d`أو` النمط: 2 [0-3]:[0-5] \ d`.
60
+
That pattern looks for `pattern:[01]\d`or `pattern:2[0-3]:[0-5]\d`.
61
61
62
-
ولكن هذا خطأ ، يجب استخدام التناوب فقط في جزء "الساعات" من التعبير العادي ، للسماح بـ "pattern:[01] \ d` OR`pattern: 2 [0-3]`. دعنا نصحح ذلك عن طريق وضع "ساعات" بين قوسين: `النمط: ([01] \ d | 2 [0-3]):[0-5] \ d`.
62
+
But that's wrong, the alternation should only be used in the "hours" part of the regular expression, to allow `pattern:[01]\d` OR`pattern:2[0-3]`. Let's correct that by enclosing "hours" into parentheses: `pattern:([01]\d|2[0-3]):[0-5]\d`.
0 commit comments