Skip to content

Alternation (OR) | #204

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 1 commit 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,7 +1,7 @@

The first idea can be to list the languages with `|` in-between.
আমরা ল্যাংগুয়েজগুলোকে অল্টারনেশন `|` দ্বারা আলাদা আলাদা লিখতে পারি।

But that doesn't work right:
কিন্তু এটি কাজ করবে না:

```js run
let regexp = /Java|JavaScript|PHP|C|C\+\+/g;
Expand All @@ -11,18 +11,18 @@ let str = "Java, JavaScript, PHP, C, C++";
alert( str.match(regexp) ); // Java,Java,PHP,C,C
```

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.
অল্টারনেশনের সময় রেগুলার এক্সপ্রেশন ইঞ্জিন এক এক করে খুঁজে। অর্থাৎ: প্রথমত এটি `match:Java` চেক করবে, যদি না মিলে এরপর `match:JavaScript` এভাবে চলতে থাকে।

As a result, `match:JavaScript` can never be found, just because `match:Java` is checked first.
যার ফলে, `match:JavaScript` কখনোই খুঁজে পাবে না, কেননা এটি শুরুতেই `match:Java` এর সাথে মিল হয়।

The same with `match:C` and `match:C++`.
অনুরূপভাবে `match:C` এবং `match:C++`

There are two solutions for that problem:
এ সমস্যার দুটি সমাধান আছে:

1. Change the order to check the longer match first: `pattern:JavaScript|Java|C\+\+|C|PHP`.
2. Merge variants with the same start: `pattern:Java(Script)?|C(\+\+)?|PHP`.
১. আমাদের প্যাটার্নটিকে এভাবে সাজিয়ে: `pattern:JavaScript|Java|C\+\+|C|PHP`
২. অথবা একই নামগুলোর জন্য অপশনাল গ্রুপ মার্জ করে: `pattern:Java(Script)?|C(\+\+)?|PHP`

In action:
যেমন:

```js run
let regexp = /Java(Script)?|C(\+\+)?|PHP/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Find programming languages
# প্রোগ্রামিং ল্যাংগুয়েজ অনুসন্ধান

There are many programming languages, for instance Java, JavaScript, PHP, C, C++.
বর্তমানে অনেক প্রোগ্রামিং ল্যাংগুয়েজ আছে, যেমন Java, JavaScript, PHP, C, C++

Create a regexp that finds them in the string `subject:Java JavaScript PHP C++ C`:
একটি রেগুলার এক্সপ্রেশন লিখুন যা দ্বারা এদের খুঁজে পাওয়া যায় `subject:Java JavaScript PHP C++ C`:

```js
let regexp = /your regexp/g;
let regexp = /আপনার প্যাটার্ন/g;

alert("Java JavaScript PHP C++ C".match(regexp)); // Java JavaScript PHP C++ C
```
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

Opening tag is `pattern:\[(b|url|quote)\]`.
শুরুর ট্যাগটি হবে `pattern:\[(b|url|quote)\]`

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.
তারপর ট্যাগটি শেষ হওয়ার পূর্ব পর্যন্ত সকল কন্টেন্ট পেতে এটি `pattern:.*?` লিখি এবং নতুন লাইন সহ সকল ক্যারাক্টারের জন্য এই ফ্ল্যাগটি `pattern:s` ব্যবহার করি, অতঃপর শেষ ট্যাগটি লিখার জন্য ব্যাকরেফারেন্স ব্যবহার করি।

The full pattern: `pattern:\[(b|url|quote)\].*?\[/\1\]`.
সম্পূর্ন প্যাটার্নটি হবে: `pattern:\[(b|url|quote)\].*?\[/\1\]`

In action:
যেমন:

```js run
let regexp = /\[(b|url|quote)\].*?\[\/\1\]/gs;
Expand All @@ -20,4 +20,4 @@ let str = `
alert( str.match(regexp) ); // [b]hello![/b],[quote][url]http://google.com[/url][/quote]
```

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.
আমরা এই বন্ধনী `pattern:[` এবং `pattern:]` এর পূর্বে ব্যাকস্ল্যাশ দ্বারা এস্কেপিং করেছি, অনুরূপভাবে শেষ ট্যাগটিকেও `pattern:[\/\1]` এস্কেপিং করা লাগবে, কেননা স্ল্যাশ দ্বারা প্যাটার্নের শেষ বুঝায়।
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
# Find bbtag pairs
# bbtag অনুসন্ধান

A "bb-tag" looks like `[tag]...[/tag]`, where `tag` is one of: `b`, `url` or `quote`.
"bb-tag" দেখতে এমন `[tag]...[/tag]`, যেখানে `tag` গুলো হতে পারে: `b`, `url` বা `quote`

For instance:
উদাহরণস্বরূপ:
```
[b]text[/b]
[url]http://google.com[/url]
```

BB-tags can be nested. But a tag can't be nested into itself, for instance:
BB-tags একটার মধ্যে আরেকটা হতে পারে। কিন্তু একই ট্যাগের ভেতর নেস্টেড হবে না, যেমন:

```
Normal:
সঠিক:
[url] [b]http://google.com[/b] [/url]
[quote] [b]text[/b] [/quote]

Can't happen:
ভুল:
[b][b]text[/b][/b]
```

Tags can contain line breaks, that's normal:
সাধারণত ট্যাগের মধ্যে লাইন ব্রেক থাকতে পারে:

```
[quote]
[b]text[/b]
[/quote]
```

Create a regexp to find all BB-tags with their contents.
একটি রেগুলার এক্সপ্রেশন লিখুন যা দ্বারা সকল BB-tags এবং এদের কন্টেন্ট খুঁজে পাওয়া যায়।

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

```js
let regexp = /your regexp/flags;
let regexp = /আপনার রেগুলার এক্সপ্রেশন/ফ্ল্যাগস;

let str = "..[url]http://google.com[/url]..";
alert( str.match(regexp) ); // [url]http://google.com[/url]
```

If tags are nested, then we need the outer tag (if we want we can continue the search in its content):
যদি ট্যাগটি নেস্টেড হয়, তবে আমরা প্যারেন্ট ট্যাগটি খুঁজব(তাহলে আমরা পরবর্তীতে এর মধ্যের কন্টেন্টগুলোতে অনুসন্ধান চালাতে পারব):

```js
let regexp = /your regexp/flags;
let regexp = /আপনার রেগুলার এক্সপ্রেশন/ফ্ল্যাগস;;

let str = "..[url][b]http://google.com[/b][/url]..";
alert( str.match(regexp) ); // [url][b]http://google.com[/b][/url]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
The solution: `pattern:/"(\\.|[^"\\])*"/g`.
সমাধানটি হবে: `pattern:/"(\\.|[^"\\])*"/g`

Step by step:
ধাপে ধাপে দেখি:

- First we look for an opening quote `pattern:"`
- 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).
- 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:[^"\\]`
- ...And so on till the closing quote.
- প্রথমে আমরা একটি শুরুর উদ্ধৃতি চিহ্নটি খুঁজব `pattern:"`
- এরপর আমরা ব্যাকস্ল্যাশ খুঁজব `pattern:\\` (এজন্য আমাদের দুটি ব্যাকস্ল্যাশ দিতে হবে, কেননা এটি একটি স্পেশাল ক্যারাক্টার), এরপর যেকোন ক্যারাক্টারের জন্য(একটি ডট)।
- অন্যথায় আমরা উদ্ধৃতি চিহ্ন এবং ব্যাকস্ল্যাশ ব্যতীত সকল ক্যারাক্টার নিব: `pattern:[^"\\]`
- ...এবং সর্বশেষ শেষ উদ্ধৃতি চিহ্ন।

In action:
সমাধানটি হবে:

```js run
let regexp = /"(\\.|[^"\\])*"/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# Find quoted strings
# স্ট্রিংয়ে উক্তি খুঁজা

Create a regexp to find strings in double quotes `subject:"..."`.
একটি রেগুলার এক্সপ্রেশন লিখুন যা স্ট্রিংয়ে উক্তি খুঁজে `subject:"..."`

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:\\`.
স্ট্রিংটি অবশ্যই জাভাস্ক্রিপ্ট স্ট্রিংয়ের মত এস্কেপিং সাপোর্ট করবে, উক্তিটির মধ্যে উদ্ধৃতি চিহ্ন `subject:\"` বা নিউলাইন ক্যারাক্টার থাকবে `subject:\n` এবং স্ল্যাশ `subject:\\` থাকবে।

```js
let str = "Just like \"here\".";
```

Please note, in particular, that an escaped quote `subject:\"` does not end a string.
আমাদের মনে রাখা উচিত যে, উক্তির মাঝে উদ্ধৃতি চিহ্ন থাকলে `subject:\"` তা দ্বারা বুঝায় উক্তিটি শেষ হয়নি।

So we should search from one quote to the other ignoring escaped quotes on the way.
সুতরাং অনুসন্ধানের সময় আমাদের উক্তির মাঝে এস্কেপিং উদ্ধৃতি চিহ্নগুলোও খুঁজতে হবে।

That's the essential part of the task, otherwise it would be trivial.
এটিই আমাদের এই টাস্কের জন্য কঠিন অংশ, অন্যথায় এটি একটি সহজ টাস্ক।

Examples of strings to match:
উদাহরণস্বরূপ এইগুলো দেখুন:
```js
.. *!*"test me"*/!* ..
.. *!*"Say \"Hello\"!"*/!* ... (escaped quotes inside)
.. *!*"\\"*/!* .. (double slash inside)
.. *!*"\\ \""*/!* .. (double slash and an escaped quote inside)
.. *!*"test me"*/!* ..
.. *!*"Say \"Hello\"!"*/!* ... (উক্তির ভেতরে এস্কেপিং উদ্ধৃতি চিহ্ন)
.. *!*"\\"*/!* .. (উক্তির ভেতরে দুটি স্ল্যাশ)
.. *!*"\\ \""*/!* .. (উক্তির ভেতরে দুটি স্ল্যাশ এবং এস্কেপিং উদ্ধৃতি চিহ্ন)
```

In JavaScript we need to double the slashes to pass them right into the string, like this:
জাভাস্ক্রিপ্টে স্ট্রিংয়ের মাঝে ডাবল স্ল্যাশ এভাবে লিখতে হবে:

```js run
let str = ' .. "test me" .. "Say \\"Hello\\"!" .. "\\\\ \\"" .. ';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

The pattern start is obvious: `pattern:<style`.
প্যাটার্নটি শুরু হবে: `pattern:<style`

...But then we can't simply write `pattern:<style.*?>`, because `match:<styler>` would match it.
...কিন্তু আমরা এটিকে এভাবে `pattern:<style.*?>` লিখতে পারব না, কেননা `match:<styler>` এর সাথেও মিলবে।

We need either a space after `match:<style` and then optionally something else or the ending `match:>`.
`match:<style` এর পর আমাদের একটি স্পেস লাগবে এবং এর পর তাদের অপশনাল অ্যাট্রিবিউট লাগবে এবং শেষ হবে `match:>` দ্বারা।

In the regexp language: `pattern:<style(>|\s.*?>)`.
সুতরাং প্যাটার্নটি হবে: `pattern:<style(>|\s.*?>)`

In action:
সমাধানটি হবে:

```js run
let regexp = /<style(>|\s.*?>)/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Find the full tag
# সম্পূর্ন ট্যাগ অনুসন্ধান

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="...">`.
এই ট্যাগটি খুঁজার `<style...>` একটি রেগুলার এক্সপ্রেশন লিখুন। এটি অবশ্যই সম্পূর্ণ ট্যাগটির সাথে মিলবে: এটি অ্যাট্রিবিউট ছাড়াও হতে পারে `<style>` আবার এর একাধিক অ্যাট্রিবিউট থাকতে পারে `<style type="..." id="...">`

...But the regexp should not match `<styler>`!
...কিন্তু এটি এর সাথে `<styler>` মিলবে না!

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

```js
let regexp = /your regexp/g;
let regexp = /আপনার প্যাটার্ন/g;

alert( '<style> <styler> <style test="...">'.match(regexp) ); // <style>, <style test="...">
```
Loading