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
Copy file name to clipboardExpand all lines: 9-regular-expressions/10-regexp-greedy-and-lazy/article.md
+38-37Lines changed: 38 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,21 @@
1
-
# Greedy and lazy quantifiers
1
+
# Greedy এবং lazy কোয়ান্টিফায়ার
2
2
3
-
Quantifiers are very simple from the first sight, but in fact they can be tricky.
3
+
প্রথম দেখায় কোয়ান্টিফায়ারকে সহজ মনে হতে পারে, কিন্তু বাস্তবে এরা কৌশলী।
4
4
5
-
We should understand how the search works very well if we plan to look for something more complex than `pattern:/\d+/`.
5
+
জটিল কিছু খোঁজার পূর্বে আমাদের বুঝতে হবে `pattern:/\d+/` এরা কিভাবে কাজ করে।
6
6
7
-
Let's take the following task as an example.
7
+
উদাহরণ হিসাবে নিচের টাস্কটি নিয়ে কাজ করি।
8
8
9
-
We have a text and need to replace all quotes `"..."` with guillemet marks: `«...»`. They are preferred for typography in many countries.
9
+
আমাদের কাছে একটি টেক্সট আছে এবং আমরা সকল উদ্ধৃতি চিহ্নকে `"..."`
10
+
গিলিমেট চিহ্ন `«...»` দিয়ে প্রতিস্থাপন করব। অনেক দেশে টাইপোগ্রাফির জন্য এগুলোই বেশি পছন্দের।
10
11
11
-
For instance: `"Hello, world"`should become `«Hello, world»`. There exist other quotes, such as `„Witam, świat!”` (Polish) or`「你好,世界」` (Chinese), but for our task let's choose`«...»`.
12
+
উদাহরণস্বরূপ: `"Hello, world"`হবে `«Hello, world»`. আরো অনেক ধরণের উদ্ধৃতি চিহ্ন আছে, যেমন `„Witam, świat!”` (পোলিশ) অথবা`「你好,世界」` (চাইনিজ), কিন্ত আমাদের টাস্কের জন্য এখন এটিই`«...»` যুতসই।
12
13
13
-
The first thing to do is to locate quoted strings, and then we can replace them.
14
+
আমাদের প্রথম কাজটি হল স্ট্রিং হতে উদ্ধৃতি চিহ্নগুলোকে চিহ্নিত করা, এবং তারপর এদের আমরা প্রতিস্থাপন করতে পারব।
14
15
15
-
A regular expression like `pattern:/".+"/g` (a quote, then something, then the other quote) may seem like a good fit, but it isn't!
16
+
রেগুলার এক্সপ্রেশনে আমাদের প্যাটার্নটি হবে এমন `pattern:/".+"/g` (শুরুর উদ্ধৃতি চিহ্ন, তারপর কিছু ক্যারাক্টার, অতঃপর শেষ উদ্ধৃতি চিহ্ন) দেখতে সঠিক মনে হচ্ছে, তবে এটি সঠিক নই!
16
17
17
-
Let's try it:
18
+
চেষ্টা করা যাক:
18
19
19
20
```js run
20
21
let regexp =/".+"/g;
@@ -24,75 +25,75 @@ let str = 'a "witch" and her "broom" is one';
24
25
alert( str.match(regexp) ); // "witch" and her "broom"
25
26
```
26
27
27
-
...We can see that it works not as intended!
28
+
...আমরা দেখতে পাচ্ছি এটি আমাদের চাহিদামত কাজ করছে না!
28
29
29
-
Instead of finding two matches `match:"witch"`and`match:"broom"`, it finds one: `match:"witch" and her "broom"`.
30
+
`match:"witch"`এবং`match:"broom"` এই দুটি মিল খুঁজে পাওয়ার পরিবর্তে এটি দেখায়: `match:"witch" and her "broom"`।
30
31
31
32
That can be described as "greediness is the cause of all evil".
32
33
33
34
## Greedy search
34
35
35
-
To find a match, the regular expression engine uses the following algorithm:
36
+
কোন একটি মিল খুঁজতে রেগুলার এক্সপ্রেশন ইঞ্জিন নিম্নে উল্লেখিত অ্যালগরিদম অনুযায়ী কাজ করে:
36
37
37
-
-For every position in the string
38
-
-Try to match the pattern at that position.
39
-
-If there's no match, go to the next position.
38
+
-স্ট্রিংয়ের প্রতিটি অবস্থানের জন্য
39
+
-অই অবস্থানে প্যাটার্নটির অবস্থান মেলানোর চেষ্টা করে।
40
+
-যদি কোন মিল না পায়, তাহলে পরের অবস্থানে যায়।
40
41
41
-
These common words do not make it obvious why the regexp fails, so let's elaborate how the search works for the pattern `pattern:".+"`.
42
+
উপরোল্লিখিত নিয়ম থেকে রেগুলার এক্সপ্রেশন কিভাবে কাজ করে তা আমাদের কাছে সহজে বোধগম্য নই, চলুন এই প্যাটার্নটি `pattern:".+"` কিভাবে মিল খুঁজে তা বিস্তারিত দেখি।
42
43
43
-
1.The first pattern character is a quote`pattern:"`.
44
+
1.আমাদের প্যাটার্নের প্রথম ক্যারাক্টারটি একটি উদ্ধৃতি চিহ্ন`pattern:"`।
44
45
45
-
The regular expression engine tries to find it at the zero position of the source string `subject:a "witch" and her "broom" is one`, but there's `subject:a` there, so there's immediately no match.
46
+
রেগুলার এক্সপ্রেশন ইঞ্জিন প্রদত্ত স্ট্রিংয়ের `subject:a "witch" and her "broom" is one` শূন্যতম অবস্থানে একটি উদ্ধৃতি চিহ্ন মেলানোর চেষ্টা করে, কিন্তু অই অবস্থানে ক্যারাক্টারটি হল `subject:a`, সুতরাং কোন মিল খুঁজে পাবে না।
46
47
47
-
Then it advances: goes to the next positions in the source string and tries to find the first character of the pattern there, fails again, and finally finds the quote at the 3rd position:
48
+
তারপর এটি এর পরবর্তী অবস্থানে যাবে: অই অবস্থানে প্রদত্ত স্ট্রিংয়ের সাথে প্যাটার্নের প্রথম ক্যারাক্টারটি মেলানোর চেষ্টা করে, এবং এটি মেলে না, অবশেষে ৩য় অবস্থানে এটি উদ্ধৃতি চিহ্নের সাথে মেলে:
48
49
49
50

50
51
51
-
2.The quote is detected, and then the engine tries to find a match for the rest of the pattern. It tries to see if the rest of the subject string conforms to `pattern:.+"`.
52
+
2.উদ্ধৃতি চিহ্নের সাথে মিল হওয়ার পর, ইঞ্জিন বাকি প্যাটার্নটির মিল খুঁজে। ইঞ্জিন প্রদত্ত স্ট্রিংয়ের সাথে আমাদের প্যাটার্নের `pattern:.+"` মিল খুঁজে।
52
53
53
-
In our case the next pattern character is `pattern:.` (a dot). It denotes "any character except a newline", so the next string letter `match:'w'`fits:
54
+
এইক্ষত্রে, আমাদের প্যাটার্নের পরবর্তী ক্যারাক্টারটি হল `pattern:.` (একটি ডট ক্যারাক্টার)। এটি দ্বারা বুঝায় "নতুন লাইন ব্যাতীত সকল ক্যারাক্টার", সুতরাং স্ট্রিংয়ের পরবর্তী বর্ণ `match:'w'`এর সাথে মিল খুঁজে পায়:
54
55
55
56

56
57
57
-
3.Then the dot repeats because of the quantifier `pattern:.+`. The regular expression engine adds to the match one character after another.
58
+
3.এরপর ডটের সাথে কোয়ান্টিফায়ার `pattern:.+` থাকার কারনে রেগুলার এক্সপ্রেশন ইঞ্জিন পরবর্তী ক্যারাক্টার গুলোর সাথে মিলতে থাকে।
58
59
59
-
...Until when? All characters match the dot, so it only stops when it reaches the end of the string:
60
+
...কিন্তু কতক্ষণ পর্যন্ত? স্ট্রিংয়ের শেষ পর্যন্ত এর সকল সকল ক্যারাক্টার ডটের সাথে মেলতে থাকে:
60
61
61
62

62
63
63
-
4.Now the engine finished repeating `pattern:.+`and tries to find the next character of the pattern. It's the quote `pattern:"`. But there's a problem: the string has finished, there are no more characters!
64
+
4.এখন ইঞ্জিন এই প্যাটার্নের `pattern:.+`পুনরাবৃত্তি শেষে পরের প্যাটার্নটি খুঁজে। এটি হল উদ্ধৃতি চিহ্ন `pattern:"`। কিন্ত এখানে একটি সমস্যা আছে: প্যাটার্নের অবস্থান প্রদত্ত স্ট্রিংটির শেষে, এরপর আর কোন ক্যারাক্টার নেয়!
64
65
65
-
The regular expression engine understands that it took too many `pattern:.+`and starts to *backtrack*.
66
+
রেগুলার এক্সপ্রেশন ইঞ্জিন বুঝতে পারে `pattern:.+`অনেক বেশী অগ্রসর হয়ে গেছে সুতরাং এটি মিলটিকে *ব্যাকট্রাক* বা প্রত্যাখ্যান করা শুরু করে।
66
67
67
-
In other words, it shortens the match for the quantifier by one character:
68
+
অন্যথায় বলা যায়, কোয়ান্টিফায়ারের জন্য একটি ক্যারাক্টার বাদ দেয়:
68
69
69
70

70
71
71
-
Now it assumes that`pattern:.+`ends one character before the string end and tries to match the rest of the pattern from that position.
72
+
এখন এটি ধরা যাক`pattern:.+`শেষ। স্ট্রিংয়ের শেষ অবস্থান হতে একটি ক্যারাক্টার নেয় এবং সেই অবস্থান থেকে বাকী প্যাটার্নটি মেলাতে চেষ্টা করে।
72
73
73
-
If there were a quote there, then the search would end, but the last character is `subject:'e'`, so there's no match.
74
+
যদি অই অবস্থানে উদ্ধৃতি চিহ্ন থাকে তবে আমাদের অনুসদ্ধানটি শেষ হবে, কিন্তু শেষ ক্যারাক্টারটি হল `subject:'e'`, সুতরাং কোন মিল হবেনা।
74
75
75
-
5. ...So the engine decreases the number of repetitions of `pattern:.+`by one more character:
76
+
5. ...সুতরাং ইঞ্জিন `pattern:.+`এর জন্য আর একটি ক্যারাক্টার কমাবে:
76
77
77
78

78
79
79
-
The quote `pattern:'"'`does not match `subject:'n'`.
80
+
কিন্তু `pattern:'"'`উদ্ধৃতি চিহ্ন `subject:'n'` এর সাথে মেলেনা।
80
81
81
-
6.The engine keep backtracking: it decreases the count of repetition for `pattern:'.'`until the rest of the pattern (in our case `pattern:'"'`) matches:
82
+
6.ইঞ্জিনটি ব্যাকট্র্যাকিং চালিয়ে যায়: এটি `pattern:'.'`এর জন্য ক্রমাগত অনুসন্ধানকৃত ফলাফলটিকে সংক্ষিপ্ত করতে থাকে যতক্ষণ পর্যন্ত আমাদের প্যাটার্নটির (এইক্ষেত্রে `pattern:'"'`) মিল শেষ হয়:
82
83
83
84

84
85
85
-
7.The match is complete.
86
+
7.অনুসন্ধানটি সম্পূর্ন হয়।
86
87
87
-
8.So the first match is`match:"witch" and her "broom"`. If the regular expression has flag `pattern:g`, then the search will continue from where the first match ends. There are no more quotes in the rest of the string `subject:is one`, so no more results.
88
+
8.সুতরাং আমাদের প্রথম অনুসন্ধানটি হল`match:"witch" and her "broom"`। যদি রেগুলার এক্সপ্রেশনে `pattern:g` ফ্ল্যাগটি থাকে, তাহলে অনুসন্ধানটি চলবে যেখানে প্রথম মিলটি শেষ হয়। বাকী স্ট্রিংয়ে `subject:is one` আর কোন উদ্ধৃতি চিহ্ন নেয়, সুতরাং অন্য কোন ফলাফল আসবে না।
88
89
89
-
That's probably not what we expected, but that's how it works.
90
+
যদিওবা এটি আমাদের চাহিদামত কাজ করেনি, কিন্তু আমরা বুঝতে পারছি এটি কিভাবে কাজ করে।
90
91
91
-
**In the greedy mode (by default) a quantifier is repeated as many times as possible.**
92
+
**গ্রীডি অবস্থায়(ডিফল্ট ভাবে) একটি কোয়ান্টিফায়ার যতবার সম্ভব এর পুনরাবৃত্তি ঘটায়।**
92
93
93
-
The regexp engine adds to the match as many characters as it can for `pattern:.+`, and then shortens that one by one, if the rest of the pattern doesn't match.
94
+
রেগুলার এক্সপ্রেশন ইঞ্জিন যত সম্ভব ক্যারাক্টার `pattern:.+` এর জন্য সংযোজন করে, এবং এরপর বাকী প্যাটার্ন না মিললে একটির পর একটি ক্যারাক্টার বাদ দিতে থাকে।
94
95
95
-
For our task we want another thing. That's where a lazy mode can help.
96
+
আমাদের টাস্কের জন্য আরো একটি বিষয় জানা উচিত। এই ক্ষেত্রে আমরা লেজি মোডের সাহায্য নিতে পারি।
0 commit comments