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

`m` পতাকা: বহুরেখা

With the m (multiline) flag the anchors ^ and $ change meaning: they are no longer start/end of the entire string, but start/end of each line. A line is delimited by \n (or by the start/end of the string).

Code
Pattern: ^Errore
Flag:    gm
Sample:  Errore: A\nInfo: B\nErrore: C
         ^^^^^^         ^^^^^^

Without m it would match only Errore at the absolute start of the sample (1 match). With m, it matches the start of every line (2 matches).

m does NOT change the dot

A common confusion: the m flag does NOT change the meaning of the dot .. The dot still does NOT match \n even with m. To make the dot match everything you need the s flag (covered in module 1).

m and s are orthogonal: you can use them together (ms), separately, or neither.

Multiline flag and line-by-line processing

The m flag modifies the behavior of ^ and $ by making them match the start and end of each line (delimited by \\n) within a multiline text block, instead of looking only at the absolute start and end of the entire string.

Try it

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

Find every line that starts with `ERROR`. Each line is separated by \\n; use flags `g` and `m`.

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

The m flag makes ^ match the start of inner lines too, not only the absolute first character.

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

Review exercise

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

Find every line that ENDS with a period. Use the `$` anchor with flags `m` and `g`.

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

\\.$ with gm flags matches the period at the end of every line, not only at the end of the sample.

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

Additional challenge

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

Find all lines in a multiline log that start exactly with the string `ID:` followed by a digit.

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

Use ^ before ID: and make sure the m flag is active.

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