使用Erlang re模块的多行正则表达式

lht*_*lht 2 regex erlang

无法让Erlang重新为多线工作,请帮忙!

> 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}]}
Run Code Online (Sandbox Code Playgroud)

Bar*_*ers 7

multiline选项只告诉正则表达式引擎^不仅将其视为字符串的开头,而且还将其视为新行的开头,它还告诉引擎$不仅要将其视为字符串的结尾,还要将其视为结尾.线.

试试这个:

re:run("hello,\nworld", "o,.*w", [dotall]) 
Run Code Online (Sandbox Code Playgroud)

dotall选项将告诉正则表达式引擎也允许换行符与DOT元字符匹配.