Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline Regular Expression with Erlang re module

Tags:

regex

erlang

Failed to get Erlang re work for multiline, please help!

> re:run("hello,\nworld", "o,.*w", [multiline]).
nomatch
> re:run("hello,\nworld", "o,.*w", [multiline, {newline, lf}]).
nomatch

> {ok, MP} = re:compile("o,.*w", [multiline]).
{ok,{re_pattern,0,0,
                <<69,82,67,80,55,0,0,0,2,0,0,0,7,0,0,0,0,0,0,0,111,0,
                  119,...>>}}
> re:run("hello,\nworld", MP).
nomatch

> re:run("hello,\nworld", ",\nw").
{match,[{5,3}]}
like image 403
lht Avatar asked Apr 15 '26 11:04

lht


2 Answers

The multiline option only tells the regex engine to treat ^ not only as the start of the string, but also as the start of a new line and it also tells the engine to treat $ not only as the end of the string, but as the end of a line.

Try this instead:

re:run("hello,\nworld", "o,.*w", [dotall]) 

The dotall option will tell the regex engine to also let line breaks be matched by the DOT meta character.

like image 164
Bart Kiers Avatar answered Apr 17 '26 01:04

Bart Kiers


use the dotall option, i.e.

> re:run("hello,\nworld", "o,.*w", [dotall]).
{match,[{4,4}]}
like image 31
Lukas Avatar answered Apr 17 '26 03:04

Lukas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!