Cornwell
Cornwell

Reputation: 3410

Matching multi-line text

I have the following text:

[FONT=arial, helvetica]this

is a multiline text

[/FONT]

[FONT=arial, helvetica] this is a single line comment[/FONT]

I'm trying to match the text inside the bbcode tags with this pattern:

\[FONT=(.*?)\](.*?)\[\/FONT\]\im

But it only matches the single line text, why? I've even added the multi-line flag.

http://www.regexr.com/3b72f

Upvotes: 2

Views: 72

Answers (2)

vks
vks

Reputation: 67998

\[FONT=([\s\S]*?)\]([\s\S]*?)\[\/FONT\]

You can also use this without any flags.See demo.

https://regex101.com/r/hI0qP0/28

Upvotes: 2

Andrey Korneyev
Andrey Korneyev

Reputation: 26896

Instead of multiline flag you need single-line here.

/\[FONT=(.*?)\](.*?)\[\/FONT\]/isg

Single-line modifier makes dot symbol match newline characters as well - it is exactly your issue.

Multiline modifier only makes beginning/end characters (^ and $) match start/end of line.

Upvotes: 3

Related Questions