正则表达式匹配EOF [英] regex to match EOF

查看:84
本文介绍了正则表达式匹配EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些看起来像这样的数据

约翰、戴夫、克里斯瑞克,山姆,鲍勃乔、米尔特、保罗

我正在使用这个正则表达式来匹配名称

/(\w.+?)(\r\n|\n|,)/

大部分情况下有效,但文件在最后一个单词之后突然结束,这意味着最后一个值不以 \r\n\n, 以EOF结尾.有没有办法在正则表达式中匹配 EOF,这样我就可以把它放在第二个分组中?

解决方案

这个问题的答案是 \Z 我花了一些时间才弄明白,但现在可以了.请注意,相反,\A 匹配整个字符串的开头(而不是 ^$ 匹配一行的开头).

I have some data that look like this

john, dave, chris
rick, sam, bob
joe, milt, paul

I'm using this regex to match the names

/(\w.+?)(\r\n|\n|,)/

which works for the most part but the file ends abruptly after the last word meaning the last value doesn't end in \r\n, \n or , it ends with EOF. Is there a way to match EOF in regex so I can put it right in that second grouping?

解决方案

The answer to this question is \Z took me awhile to figure it out, but it works now. Note that conversely, \A matches beginning of the whole string (as opposed to ^ and $ matching the beginning of one line).

这篇关于正则表达式匹配EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆