Skip to content

Commit a1a7ca0

Browse files
committed
translate greedy mode section
1 parent 10910e1 commit a1a7ca0

File tree

1 file changed

+38
-37
lines changed
  • 9-regular-expressions/10-regexp-greedy-and-lazy

1 file changed

+38
-37
lines changed

9-regular-expressions/10-regexp-greedy-and-lazy/article.md

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
# Greedy and lazy quantifiers
1+
# Greedy এবং lazy কোয়ান্টিফায়ার
22

3-
Quantifiers are very simple from the first sight, but in fact they can be tricky.
3+
প্রথম দেখায় কোয়ান্টিফায়ারকে সহজ মনে হতে পারে, কিন্তু বাস্তবে এরা কৌশলী।
44

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+/` এরা কিভাবে কাজ করে।
66

7-
Let's take the following task as an example.
7+
উদাহরণ হিসাবে নিচের টাস্কটি নিয়ে কাজ করি।
88

9-
We have a text and need to replace all quotes `"..."` with guillemet marks: `«...»`. They are preferred for typography in many countries.
9+
আমাদের কাছে একটি টেক্সট আছে এবং আমরা সকল উদ্ধৃতি চিহ্নকে `"..."`
10+
গিলিমেট চিহ্ন `«...»` দিয়ে প্রতিস্থাপন করব। অনেক দেশে টাইপোগ্রাফির জন্য এগুলোই বেশি পছন্দের।
1011

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!”` (পোলিশ) অথবা `「你好,世界」` (চাইনিজ), কিন্ত আমাদের টাস্কের জন্য এখন এটিই `«...»` যুতসই।
1213

13-
The first thing to do is to locate quoted strings, and then we can replace them.
14+
আমাদের প্রথম কাজটি হল স্ট্রিং হতে উদ্ধৃতি চিহ্নগুলোকে চিহ্নিত করা, এবং তারপর এদের আমরা প্রতিস্থাপন করতে পারব।
1415

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` (শুরুর উদ্ধৃতি চিহ্ন, তারপর কিছু ক্যারাক্টার, অতঃপর শেষ উদ্ধৃতি চিহ্ন) দেখতে সঠিক মনে হচ্ছে, তবে এটি সঠিক নই!
1617

17-
Let's try it:
18+
চেষ্টা করা যাক:
1819

1920
```js run
2021
let regexp = /".+"/g;
@@ -24,75 +25,75 @@ let str = 'a "witch" and her "broom" is one';
2425
alert( str.match(regexp) ); // "witch" and her "broom"
2526
```
2627

27-
...We can see that it works not as intended!
28+
...আমরা দেখতে পাচ্ছি এটি আমাদের চাহিদামত কাজ করছে না!
2829

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"`
3031

3132
That can be described as "greediness is the cause of all evil".
3233

3334
## Greedy search
3435

35-
To find a match, the regular expression engine uses the following algorithm:
36+
কোন একটি মিল খুঁজতে রেগুলার এক্সপ্রেশন ইঞ্জিন নিম্নে উল্লেখিত অ্যালগরিদম অনুযায়ী কাজ করে:
3637

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+
- যদি কোন মিল না পায়, তাহলে পরের অবস্থানে যায়।
4041

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:".+"` কিভাবে মিল খুঁজে তা বিস্তারিত দেখি।
4243

43-
1. The first pattern character is a quote `pattern:"`.
44+
1. আমাদের প্যাটার্নের প্রথম ক্যারাক্টারটি একটি উদ্ধৃতি চিহ্ন `pattern:"`
4445

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`, সুতরাং কোন মিল খুঁজে পাবে না।
4647

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+
তারপর এটি এর পরবর্তী অবস্থানে যাবে: অই অবস্থানে প্রদত্ত স্ট্রিংয়ের সাথে প্যাটার্নের প্রথম ক্যারাক্টারটি মেলানোর চেষ্টা করে, এবং এটি মেলে না, অবশেষে ৩য় অবস্থানে এটি উদ্ধৃতি চিহ্নের সাথে মেলে:
4849

4950
![](witch_greedy1.svg)
5051

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:.+"` মিল খুঁজে।
5253

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'` এর সাথে মিল খুঁজে পায়:
5455

5556
![](witch_greedy2.svg)
5657

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:.+` থাকার কারনে রেগুলার এক্সপ্রেশন ইঞ্জিন পরবর্তী ক্যারাক্টার গুলোর সাথে মিলতে থাকে।
5859

59-
...Until when? All characters match the dot, so it only stops when it reaches the end of the string:
60+
...কিন্তু কতক্ষণ পর্যন্ত? স্ট্রিংয়ের শেষ পর্যন্ত এর সকল সকল ক্যারাক্টার ডটের সাথে মেলতে থাকে:
6061

6162
![](witch_greedy3.svg)
6263

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:"`। কিন্ত এখানে একটি সমস্যা আছে: প্যাটার্নের অবস্থান প্রদত্ত স্ট্রিংটির শেষে, এরপর আর কোন ক্যারাক্টার নেয়!
6465

65-
The regular expression engine understands that it took too many `pattern:.+` and starts to *backtrack*.
66+
রেগুলার এক্সপ্রেশন ইঞ্জিন বুঝতে পারে `pattern:.+` অনেক বেশী অগ্রসর হয়ে গেছে সুতরাং এটি মিলটিকে *ব্যাকট্রাক* বা প্রত্যাখ্যান করা শুরু করে।
6667

67-
In other words, it shortens the match for the quantifier by one character:
68+
অন্যথায় বলা যায়, কোয়ান্টিফায়ারের জন্য একটি ক্যারাক্টার বাদ দেয়:
6869

6970
![](witch_greedy4.svg)
7071

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:.+` শেষ। স্ট্রিংয়ের শেষ অবস্থান হতে একটি ক্যারাক্টার নেয় এবং সেই অবস্থান থেকে বাকী প্যাটার্নটি মেলাতে চেষ্টা করে।
7273

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'`, সুতরাং কোন মিল হবেনা।
7475

75-
5. ...So the engine decreases the number of repetitions of `pattern:.+` by one more character:
76+
5. ...সুতরাং ইঞ্জিন `pattern:.+` এর জন্য আর একটি ক্যারাক্টার কমাবে:
7677

7778
![](witch_greedy5.svg)
7879

79-
The quote `pattern:'"'` does not match `subject:'n'`.
80+
কিন্তু `pattern:'"'` উদ্ধৃতি চিহ্ন `subject:'n'` এর সাথে মেলেনা।
8081

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:'"'`) মিল শেষ হয়:
8283

8384
![](witch_greedy6.svg)
8485

85-
7. The match is complete.
86+
7. অনুসন্ধানটি সম্পূর্ন হয়।
8687

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` আর কোন উদ্ধৃতি চিহ্ন নেয়, সুতরাং অন্য কোন ফলাফল আসবে না।
8889

89-
That's probably not what we expected, but that's how it works.
90+
যদিওবা এটি আমাদের চাহিদামত কাজ করেনি, কিন্তু আমরা বুঝতে পারছি এটি কিভাবে কাজ করে।
9091

91-
**In the greedy mode (by default) a quantifier is repeated as many times as possible.**
92+
**গ্রীডি অবস্থায়(ডিফল্ট ভাবে) একটি কোয়ান্টিফায়ার যতবার সম্ভব এর পুনরাবৃত্তি ঘটায়।**
9293

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:.+` এর জন্য সংযোজন করে, এবং এরপর বাকী প্যাটার্ন না মিললে একটির পর একটি ক্যারাক্টার বাদ দিতে থাকে।
9495

95-
For our task we want another thing. That's where a lazy mode can help.
96+
আমাদের টাস্কের জন্য আরো একটি বিষয় জানা উচিত। এই ক্ষেত্রে আমরা লেজি মোডের সাহায্য নিতে পারি।
9697

9798
## Lazy mode
9899

0 commit comments

Comments
 (0)