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

`*`, `+`, `?`

Quantifiers tell the engine how many times to repeat the element that precedes them. The three basic quantifiers are *, +, ?.

QuantifierMeaningExampleMatches
*zero or more timesba*b, ba, baa
+one or more timesba+ba, baa
?zero or one timescolou?rcolor, colour

They apply to the last atom: a single character (a+), a class (\d+), a group ((ab)+, module 4).

Code
Pattern: \d+
Sample:  Codici 7, 12 e 314.
                ^  ^^    ^^^

\d+ matches "one or more digits", it does not stop at the first one: a maximal sequence of consecutive digits.

Deep dive into the optional quantifier

The question mark ? applies only to the single character immediately preceding it. To make an entire sequence of characters or words optional, you must enclose them in parentheses, for example (https)?.

Try it

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

Find every sequence of one or more consecutive lowercase 'a's in the text.

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

Use the + quantifier (one or more).

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

Review exercise

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

Find every integer, with an optional minus sign (e.g. `42`, `-3`, `0`). Use `?` for the optional minus and `+` for the digits.

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

Prefix -? to the digit class, so the minus sign is either present or absent.

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

Additional challenge

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

Find both `color` and `colour` in the text using the optional quantifier `?`.

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

Place the ? after the letter u to indicate it can appear zero or one times.

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