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
Sometimes we need to find only those matches for a pattern that are followed or preceeded by another pattern.
3
+
অনেক সময় আমাদের এমন প্যাটার্ন খুঁজা লাগে যা অন্য প্যাটার্নের উপর নির্ভর করে।
4
4
5
-
There's a special syntax for that, called "lookahead" and "lookbehind", together referred to as "lookaround".
5
+
এজন্য রেগুলার এক্সপ্রেশনে একটি বিশেষ সিনট্যাক্স আছে "লুকঅ্যাহেড" এবং "লুকবিহাইন্ড", এদের একত্রে বলা হয় "লুকঅ্যারাউন্ড"।
6
6
7
-
For the start, let's find the price from the string like `subject:1 turkey costs 30€`. That is: a number, followed by `subject:€`sign.
7
+
চলুন একটি ব্যবহারিক উদাহরণ দেখি, `subject:1 turkey costs 30€` এ স্ট্রিং হতে আমরা দাম খুঁজে বের করব। প্যাটার্নটি হবে প্রথমে একটি `subject:€`চিহ্ন তারপর একটি সংখ্যা।
8
8
9
-
## Lookahead
9
+
## লুকঅ্যাহেড
10
10
11
-
The syntax is: `pattern:X(?=Y)`, it means "look for `pattern:X`, but match only if followed by `pattern:Y`". There may be any pattern instead of `pattern:X`and`pattern:Y`.
11
+
সিনট্যাক্সটি হবে: `pattern:X(?=Y)`, এটি দ্বারা বুঝায় এর সাথে মিলবে `pattern:X`, কেবলমাত্র এটি যদি `pattern:Y` দ্বারা শুরু হয়। `pattern:X`এবং`pattern:Y` এর পরিবর্তে যেকোন প্যাটার্ন হতে পারে।
12
12
13
-
For an integer number followed by `subject:€`, the regexp will be`pattern:\d+(?=€)`:
13
+
`subject:€` পর একটি পূর্ণ সংখ্যা হবে, সুতরাং রেগুলার এক্সপ্রেশনটি হবে`pattern:\d+(?=€)`:
14
14
15
15
```js run
16
16
let str ="1 turkey costs 30€";
17
17
18
-
alert( str.match(/\d+(?=€)/) ); // 30, the number 1 is ignored, as it's not followed by €
18
+
alert( str.match(/\d+(?=€)/) ); // 30, 1 এর সাথে মিল হবে না কেননা এর পূর্বে € নাই
19
19
```
20
20
21
-
Please note: the lookahead is merely a test, the contents of the parentheses `pattern:(?=...)`is not included in the result `match:30`.
21
+
লক্ষ্য করুন: লুকঅ্যাহেড হল একটি যাচাই পদ্ধতি, প্যারেন্টেসিসের কন্টেন্ট `pattern:(?=...)`রেজাল্টের মধ্যে আসবে না অর্থাৎ রেজাল্ট হবে `match:30`।
22
22
23
-
When we look for `pattern:X(?=Y)`, the regular expression engine finds`pattern:X`and then checks if there's `pattern:Y`immediately after it. If it's not so, then the potential match is skipped, and the search continues.
23
+
চলুন `pattern:X(?=Y)` কিভাবে কাজ করছে তা বোঝার চেষ্টা করি, রেগুলার এক্সপ্রেশন ইঞ্জিন প্রথমে খুঁজবে`pattern:X`এবং এরপর খুঁজবে `pattern:Y`আছে কিনা। যদি এটি না মেলে, তাহলে ঐ মিলগুলো বাদ দিবে, এবং অনুসন্ধান চালিয়ে যাবে।
24
24
25
-
More complex tests are possible, e.g.`pattern:X(?=Y)(?=Z)`means:
25
+
আমরা আরো জটিল অনুসন্ধানও চালাতে পারি, যেমন`pattern:X(?=Y)(?=Z)`দ্বারা বুঝায়:
26
26
27
-
1. Find`pattern:X`.
28
-
2. Check if `pattern:Y` is immediately after `pattern:X` (skip if isn't).
29
-
3. Check if `pattern:Z` is immediately after `pattern:X` (skip if isn't).
30
-
4. If both tests passed, then it's the match.
27
+
১. প্রথমে`pattern:X` খুঁজবে।
28
+
২. পরবর্তী অনুসন্ধান চালাবে যদি `pattern:X` এরপর `pattern:Y` থাকে(অন্যথায় বাদ যাবে)।
29
+
৩. পরবর্তী অনুসন্ধান চালাবে যদি `pattern:X` এরপর `pattern:Z` থাকে(অন্যথায় বাদ যাবে)।
30
+
৪. যদি উভয়ই অনুসন্ধান মিলে, তাহলে `pattern:X` রেজাল্ট দেখাবে।
31
31
32
-
In other words, such pattern means that we're looking for `pattern:X`followed by `pattern:Y`and`pattern:Z`at the same time.
32
+
অন্য কথায় বলা যায়, আমরা `pattern:X`কে এমনভাবে খুঁজছি যার পরে `pattern:Y`এবং`pattern:Z`থাকবে।
33
33
34
-
That's only possible if patterns`pattern:Y`and`pattern:Z`aren't mutually exclusive.
34
+
এটি অবশ্যই সম্ভব হবে যদি`pattern:Y`এবং`pattern:Z`পরস্পর সাংঘর্ষিক না হয়।
35
35
36
-
For example, `pattern:\d+(?=\s)(?=.*30)`looks for`pattern:\d+`only if it's followed by a space, and there's `30`somewhere after it:
36
+
যেমন, `pattern:\d+(?=\s)(?=.*30)`দ্বারা বুঝায়`pattern:\d+`এরপর একটি স্পেস থাকবে, এবং তারপর যেকোন অবস্থানে `30`থাকবে:
37
37
38
38
```js run
39
39
let str ="1 turkey costs 30€";
40
40
41
41
alert( str.match(/\d+(?=\s)(?=.*30)/) ); // 1
42
42
```
43
43
44
-
In our string that exactly matches the number `1`.
44
+
এক্ষেত্রে আমাদের প্রাপ্ত ফলাফলটি হবে `1`।
45
45
46
-
## Negative lookahead
46
+
## নেগেটিভ লুকঅ্যাহেড
47
47
48
-
Let's say that we want a quantity instead, not a price from the same string. That's a number `pattern:\d+`, NOT followed by `subject:€`.
48
+
চলুন আমরা দামের বদলে পরিমাণ খুঁজে বের করি। সুতরাং সংখ্যাটি হবে `pattern:\d+` যার পরে `subject:€` থাকবে না।
49
49
50
-
For that, a negative lookahead can be applied.
50
+
এজন্য আমরা, নেগেটিভ লুকঅ্যাহেড এর সাহায্য নিতে পারি।
51
51
52
-
The syntax is: `pattern:X(?!Y)`, it means "search `pattern:X`, but only if not followed by `pattern:Y`".
52
+
সিনট্যাক্সটি হবে: `pattern:X(?!Y)`, এটি দ্বারা বুঝায় "`pattern:X` কে খুঁজবে, কেবলমাত্র এরপর যদি `pattern:Y` না থাকে"।
53
53
54
54
```js run
55
55
let str ="2 turkeys cost 60€";
56
56
57
-
alert( str.match(/\d+(?!€)/) ); // 2 (the price is skipped)
Lookahead allows to add a condition for "what follows".
62
+
লুকঅ্যাহেড একটি শর্ত আরোপ করে "পরবর্তী অবস্থান"।
63
63
64
-
Lookbehind is similar, but it looks behind. That is, it allows to match a pattern only if there's something before it.
64
+
লুকবিহাইন্ড ও অনুরূপ, কিন্তু এটি পূর্বের অবস্থান খুঁজে। অর্থাৎ এটি মিলবে কেবল প্যাটার্নের পূর্বের কোন একটি প্যাটার্নের সাথে মিললে।
65
65
66
-
The syntax is:
67
-
-Positive lookbehind: `pattern:(?<=Y)X`, matches `pattern:X`, but only if there's `pattern:Y`before it.
68
-
-Negative lookbehind: `pattern:(?<!Y)X`, matches `pattern:X`, but only if there's no `pattern:Y`before it.
66
+
সিনট্যাক্সটি হবে:
67
+
-পজেটিভ লুকবিহাইন্ড: `pattern:(?<=Y)X`, `pattern:X` মিলবে, কেবল এরপূর্বে যদি `pattern:Y`থাকে।
68
+
-নেগেটিভ লুকবিহাইন্ড: `pattern:(?<!Y)X`, `pattern:X` মিলবে, কেবল এরপূর্বে যদি `pattern:Y`না থাকে।
69
69
70
-
For example, let's change the price to US dollars. The dollar sign is usually before the number, so to look for `$30`we'll use `pattern:(?<=\$)\d+` -- an amount preceded by `subject:$`:
70
+
যেমন, চলুন আমরা দামটিকে US ডলার চিহ্ন দ্বারা প্রকাশ করি। ডলার চিহ্ন সাধারণত সংখ্যার পূর্বে থাকে, সুতরাং `$30`এটি খুঁজতে আমাদের প্যাটার্নটি হবে `pattern:(?<=\$)\d+` -- দামের পূর্বে একটি `subject:$` চিহ্ন:
71
71
72
72
```js run
73
73
let str ="1 turkey costs $30";
74
74
75
-
//the dollar sign is escaped \$
76
-
alert( str.match(/(?<=\$)\d+/) ); // 30 (skipped the sole number)
Generally, the contents inside lookaround parentheses does not become a part of the result.
89
+
সাধারণত, লুকঅ্যারাউন্ড এর প্যারেন্টেসিস এর কন্টেন্টগুলো রেজাল্টে আসেনা।
90
90
91
-
E.g. in the pattern `pattern:\d+(?=€)`, the `pattern:€`sign doesn't get captured as a part of the match. That's natural: we look for a number`pattern:\d+`, while`pattern:(?=€)`is just a test that it should be followed by `subject:€`.
91
+
যেমন এই প্যাটার্নে `pattern:\d+(?=€)`, `pattern:€`এই চিহ্নটি আমাদের মিলের অংশ হিসেবে দেখাবে না। এটিই স্বাভাবিক: কেননা আমরা এখানে একটি সংখ্যার`pattern:\d+` খুঁজ করছি, যেখানে`pattern:(?=€)`এটি দ্বারা শুধুমাত্র আমরা নিশ্চিত করছি সংখ্যাটি `subject:€` এরপর আসবে।
92
92
93
-
But in some situations we might want to capture the lookaround expression as well, or a part of it. That's possible. Just wrap that part into additional parentheses.
93
+
কিন্তু কিছু ক্ষেত্রে আমাদের লুক অ্যারাউন্ডের এক্সপ্রেশনটিকেও ক্যাপচার করতে হতে পারে, অথবা এর একটি অংশও হতে পারে। এটিও সম্ভব। আমাদের শুধু অংশটিকে আরেকটি আলাদা প্যারেন্টেসিসের মধ্যে লিখা লাগবে।
94
94
95
-
In the example below the currency sign`pattern:(€|kr)`is captured, along with the amount:
95
+
নিচের উদাহরণে আমরা দামের পাশাপাশি কারেন্সী চিহ্নকেও`pattern:(€|kr)`ক্যাপচার করব:
96
96
97
97
```js run
98
98
let str ="1 turkey costs 30€";
99
-
let regexp =/\d+(?=(€|kr))/; //extra parentheses around €|kr
99
+
let regexp =/\d+(?=(€|kr))/; //অতিরিক্ত একটি প্যারেন্টেসিস €|kr
100
100
101
101
alert( str.match(regexp) ); // 30, €
102
102
```
103
103
104
-
And here's the same for lookbehind:
104
+
লুকবিহাইন্ডের জন্য অনুরূপ:
105
105
106
106
```js run
107
107
let str ="1 turkey costs $30";
@@ -110,21 +110,22 @@ let regexp = /(?<=(\$|£))\d+/;
110
110
alert( str.match(regexp) ); // 30, $
111
111
```
112
112
113
-
## Summary
113
+
## সারাংশ
114
114
115
-
Lookahead and lookbehind (commonly referred to as "lookaround") are useful when we'd like to match something depending on the context before/after it.
115
+
"লুকঅ্যাহেড" এবং "লুকবিহাইন্ড" (একত্রে বলা হয় "লুকঅ্যারাউন্ড") ব্যবহার উপযোগী যদি কোন অংশ এর আগে বা পরের অংশের উপর নির্ভর করে।
116
116
117
-
For simple regexps we can do the similar thing manually. That is: match everything, in any context, and then filter by context in the loop.
117
+
সাধারণ রেগুলার এক্সপ্রেশনের মাধ্যমেই আমরা একই ব্যাপার করতে পারি।
118
+
অর্থাৎ যা সবকিছুর সাথেই মিল খুঁজবে, তারপর লুপের মাধ্যমে অই কন্টেক্সট গুলো যাচাই বাচাইয়ের মাধ্যমে খুঁজতে পারি।
118
119
119
-
Remember, `str.match` (without flag `pattern:g`) and`str.matchAll` (always) return matches as arrays with `index`property, so we know where exactly in the text it is, and can check the context.
120
+
মনে রাখবেন, `str.match` (`pattern:g` ফ্ল্যাগছাড়া) এবং`str.matchAll` (সর্বদাই) মিলগুলোকে `index`অ্যারে আকারে রিটার্ন করে, সুতরাং আমরা জানি আমাদের কোন ইনডেক্সে কোন কন্টেক্সট নিয়ে যাচাই বাছাই করা লাগবে।
120
121
121
-
But generally lookaround is more convenient.
122
+
তবে লুকঅ্যারাউন্ড এর মাধ্যমে আমরা এটি আরো সহজে করতে পারি।
0 commit comments