regex to match EOF
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.+?)(rn|n|,)/
which works for the most part but the file ends abruptly after the last word meaning the last value doesn't end in rn
, 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 is not actually a character. If you have a multi-line string, then '$' will match the end of the string as well as the end of a line.
In Perl and its brethren, A
and Z
match the beginning and end of the string, totally ignoring line-breaks.
GNU extensions to POSIX regexes use `
and '
for the same things.
Contrast the behavior of Ryan's suggested Z with z:
$ perl -we 'my $corpus = "hellon"; $corpus =~ s/Z/world/g; print(":$corpus:n")' :helloworld world: $ perl -we 'my $corpus = "hellon"; $corpus =~ s/z/world/g; print(":$corpus:n")' :hello world: $
perlre sez:
Z Match only at end of string, or before newline at the end z Match only at end of string
A translation of the test case into Ruby (1.8.7, 1.9.2) behaves the same.
链接地址: http://www.djcxy.com/p/77066.html上一篇: Rails如何动态获取数据库列?
下一篇: 正则表达式来匹配EOF