Skip to content

Lookahead and lookbehind #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

The regexp for an integer number is `pattern:\d+`.
পূর্ণসংখ্যার রেগুলার এক্সপ্রেশন হল `pattern:\d+`

We can exclude negatives by prepending it with the negative lookahead: `pattern:(?<!-)\d+`.
আমরা ঋণাত্নক সংখ্যাগুলোকে বাদ দিতে পারি নেগেটিভ লুকবিহাইন্ড এর মাধ্যমে: `pattern:(?<!-)\d+`

Although, if we try it now, we may notice one more "extra" result:
সম্ভবত, আমরা এটি চেষ্টা করতে পারি, তবে এক্ষেত্রে আমরা "অতিরিক্ত" ফলাফল পাব:

```js run
let regexp = /(?<!-)\d+/g;
Expand All @@ -13,11 +13,11 @@ let str = "0 12 -5 123 -18";
console.log( str.match(regexp) ); // 0, 12, 123, *!*8*/!*
```

As you can see, it matches `match:8`, from `subject:-18`. To exclude it, we need to ensure that the regexp starts matching a number not from the middle of another (non-matching) number.
এখানে দেখতে পাচ্ছি, `subject:-18` এর মিল `match:8`। এটিকে বাদ দিতে হলে, আমাদের নিশ্চিত হতে হবে রেগুলার এক্সপ্রেশনটি অন্য সংখ্যার মাঝে যাচাই করবে না।

We can do it by specifying another negative lookbehind: `pattern:(?<!-)(?<!\d)\d+`. Now `pattern:(?<!\d)` ensures that a match does not start after another digit, just what we need.
এজন্য আমরা আরো একটি লুকবিহাইন্ড ব্যবহার করতে পারি: `pattern:(?<!-)(?<!\d)\d+`। এখন `pattern:(?<!\d)` এর দ্বারা নিশ্চিত করছি কোন ম্যাচ অন্য ডিজিটের মাঝ থেকে যাচাই করবে না।

We can also join them into a single lookbehind here:
আমরা একে একটি লুকবিহাইন্ডে সংযোগ করতে পারি:

```js run
let regexp = /(?<![-\d])\d+/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Find non-negative integers
# অঋণাত্নক পূর্ণসংখ্যার অনুসন্ধান

There's a string of integer numbers.
এখানে পূর্ণসংখ্যার একটি স্ট্রিং আছে।

Create a regexp that looks for only non-negative ones (zero is allowed).
একটি রেগুলার এক্সপ্রেশন লিখুন যা অঋণাত্নক পূর্ণসংখ্যার অনুসন্ধান করে (শূন্যও অনুসন্ধান করবে)।

An example of use:
উদাহরণ:
```js
let regexp = /your regexp/g;
let regexp = /আপনার প্যাটার্ন/g;

let str = "0 12 -5 123 -18";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
In order to insert after the `<body>` tag, we must first find it. We can use the regular expression pattern `pattern:<body.*>` for that.
`<body>` ট্যাগ ভিতরে নতুন ট্যাগ স্থাপনের জন্য, আমাদের প্রথমে এটি খুঁজে বের করতে হবে। এজন্য আমরা এই রেগুলার এক্সপ্রেশনটি ব্যবহার করব `pattern:<body.*>`

In this task we don't need to modify the `<body>` tag. We only need to add the text after it.
এই টাস্কের জন্য আমাদের `<body>` ট্যাগকে প্রতিস্থাপিত করা লাগবে না। আমাদের শুধুমাত্র ট্যাক্সটিকে সংযোগ করতে হবে।

Here's how we can do it:
এখানে দেখুন আমরা কিভাবে এটি করতে পারি:

```js run
let str = '...<body style="...">...';
Expand All @@ -11,9 +11,10 @@ str = str.replace(/<body.*>/, '$&<h1>Hello</h1>');
alert(str); // ...<body style="..."><h1>Hello</h1>...
```

In the replacement string `$&` means the match itself, that is, the part of the source text that corresponds to `pattern:<body.*>`. It gets replaced by itself plus `<h1>Hello</h1>`.

An alternative is to use lookbehind:
রিপ্লেসমেন্ট স্ট্রিংয়ে `$&` দ্বারা বুঝায় মিলের কন্টেন্টটি অর্থাৎ সোর্সের ট্যাক্সটি `pattern:<body.*>`। মিলকৃত কন্টেন্টটি অতঃপর `<h1>Hello</h1>`।

বিকল্প আরেকটি হতে পারে লুকবিহাইন্ড:

```js run
let str = '...<body style="...">...';
Expand All @@ -22,15 +23,15 @@ str = str.replace(/(?<=<body.*>)/, `<h1>Hello</h1>`);
alert(str); // ...<body style="..."><h1>Hello</h1>...
```

As you can see, there's only lookbehind part in this regexp.
এখানে দেখতে পাচ্ছি, এখানে রেগুলার এক্সপ্রেশনে শুধুমাত্র লুকবিহাইন্ডের অংশটি আছে।

It works like this:
- At every position in the text.
- Check if it's preceeded by `pattern:<body.*>`.
- If it's so then we have the match.
এটি এভাবে কাজ করছে:
- টেক্সটের প্রতিটি পজিশনে।
- যাচাই করছে পূর্বে `pattern:<body.*>` আছে কিনা।
- যদি থাকে তাহলে আমাদের মিলটি পাব।

The tag `pattern:<body.*>` won't be returned. The result of this regexp is literally an empty string, but it matches only at positions preceeded by `pattern:<body.*>`.
`pattern:<body.*>` ট্যাগটি রিটার্ন করবে না। সুতরাং রেজাল্ট হবে এম্পটি স্ট্রিং, কিন্ত ম্যাচটির অবস্থান হবে `pattern:<body.*>` এর পরের অবস্থানটি।

So we replaces the "empty line", preceeded by `pattern:<body.*>`, with `<h1>Hello</h1>`. That's the insertion after `<body>`.
সুতরা আমরা "empty line" টিকে রিপ্লেস করব `<h1>Hello</h1>` দ্বারা যার পূর্বে `pattern:<body.*>` আছে। সুতরাং নতুন ট্যাগটি হবে `<body>` এর পর।

P.S. Regexp flags, such as `pattern:s` and `pattern:i` can also useful: `pattern:/<body.*>/si`. The `pattern:s` flag makes the dot `pattern:.` match a newline character, and `pattern:i` flag makes `pattern:<body>` also match `match:<BODY>` case-insensitively.
লক্ষ্যনীয় রেগুলার এক্সপ্রেশন ফ্ল্যাগ, যেমন `pattern:s` এবং `pattern:i` দরকারী: `pattern:/<body.*>/si``pattern:s` ফ্ল্যাগটি `pattern:.` দ্বারা নিউলাইন ক্যারাক্টারকেও বুঝায়, এবং `pattern:i` ফ্ল্যাগ কেস-ইন্সেসিটিভ বুঝায় `pattern:<body>` এটি `match:<BODY>` এর সাথেও ম্যাচ করবে।
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Insert After Head
# Body এর ভিতরে H1 ট্যাগ

We have a string with an HTML Document.
HTML Document এর একটি স্ট্রিং আছে।

Write a regular expression that inserts `<h1>Hello</h1>` immediately after `<body>` tag. The tag may have attributes.
একটি রেগুলার এক্সপ্রেশন লিখুন যার মাধ্যমে `<body>` ট্যাগ এর পর `<h1>Hello</h1>` কে সংযোগ করতে পারি। ট্যাগটির একাধিক অ্যাট্রিবিউট থাকতে পারে।

For instance:
উদাহরণস্বরূপ:

```js
let regexp = /your regular expression/;

let regexp = /আপনার প্যাটার্ন/;
let str = `
<html>
<body style="height: 200px">
Expand All @@ -20,7 +19,7 @@ let str = `
str = str.replace(regexp, `<h1>Hello</h1>`);
```

After that the value of `str` should be:
এর পর `str` এর মান হবে:
```html
<html>
<body style="height: 200px"><h1>Hello</h1>
Expand Down
Loading