মূল কন্টেন্টে যান
eLearner.app
মডিউল 1 · 1-এর পাঠ 4কোর্সে 1/32~8 min
মডিউল পাঠ (1/4)

আক্ষরিক নিদর্শন

In a regex, "normal" alphanumeric characters simply match themselves: write ciao as the pattern and the engine will look for the substring ciao inside the text. Nothing more magical: a left-to-right scan position by position.

Code
Pattern: ciao
Sample:  Buongiorno, ciao mondo! Ti dico anche ciao.
                     ^^^^                        ^^^^

A match always carries two key pieces of information:

  • the matched text (here ciao);
  • the index (0-based offset) where it starts in the sample (here 12 and 38).

By default regex are case-sensitive: ciao does not match Ciao or CIAO. To ignore case add the i flag (case-insensitive).

How the engine works

The regex engine inspects the text one character at a time. When searching for the literal pattern ciao, it first looks for the letter c. Once found, it verifies if the next character is i, then a, and finally o. If any of these matches fail, the engine backtracks to the next starting position and starts looking for c again.

Literal patterns and the g flag

Without the g (global) flag the engine stops at the first match it finds and halts. With g it continues and collects every subsequent match until the end of the string. In this course we will almost always turn g on: we want to see every match present in the text.

Try it

ব্যায়াম#regex.m1.l1.e1
প্রচেষ্টা: 0লোড হচ্ছে...

Find every exact (case-sensitive) occurrence of the word `ciao` in the sample. Hint: remember the `g` flag.

সম্পাদক লোড হচ্ছে...
ইঙ্গিত দেখান

The pattern is the word itself. Uppercase letters must not match: no i flag.

সমাধান 3 প্রচেষ্টার পরে উপলব্ধ

Review exercise

ব্যায়াম#regex.m1.l1.e2
প্রচেষ্টা: 0লোড হচ্ছে...

Find every occurrence of the word `errore` in the text, ignoring upper/lower case.

সম্পাদক লোড হচ্ছে...
ইঙ্গিত দেখান

Same word as the pattern, but add the i flag (case-insensitive) alongside g.

সমাধান 3 প্রচেষ্টার পরে উপলব্ধ

Additional challenge

ব্যায়াম#regex.m1.l1.e3
প্রচেষ্টা: 0লোড হচ্ছে...

Identify and collect all exact occurrences of the word `WARNING` (uppercase, case-sensitive) in the sample log text.

সম্পাদক লোড হচ্ছে...
ইঙ্গিত দেখান

Look directly for the literal string WARNING, making sure not to activate the i flag.

সমাধান 3 প্রচেষ্টার পরে উপলব্ধ