Erlang正则表达式匹配行的结尾或文件的结尾

时间:2022-07-05 15:24:22

I've tried [\n$] and a few other things, but none seem to work in Erlang (even if they work on regexr). What's the right way to do this in Erlang (re:compile)?

我已经尝试了[\ n $]和其他一些东西,但似乎没有一个在Erlang中工作(即使它们在regexr上工作)。在Erlang中执行此操作的正确方法是什么(re:compile)?

1 个解决方案

#1


2  

Use (\n|$):

> re:run("foobar", "(\n|$)").
{match,[{6,0},{6,0}]}
> re:run("foo\nbar", "(\n|$)").
{match,[{3,1},{3,1}]}

That is, there are two alternatives: either match a newline, or the end of the string.

也就是说,有两种选择:匹配换行符或字符串结尾。

#1


2  

Use (\n|$):

> re:run("foobar", "(\n|$)").
{match,[{6,0},{6,0}]}
> re:run("foo\nbar", "(\n|$)").
{match,[{3,1},{3,1}]}

That is, there are two alternatives: either match a newline, or the end of the string.

也就是说,有两种选择:匹配换行符或字符串结尾。