tags:

views:

968

answers:

6
+1  Q: 

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.+?)(\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?

+3  A: 

The answer to this question is \z took me awhile to figure it out, but it works now :)

Ryan
+1  A: 

Assuming you are using proper modifier forcing to treat string as a whole (not line-by-line - and if \n works for you, you are using it), just add another alternative - end of string: (\r\n|\n|,|$)

leafnode
A: 

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

cube
+3  A: 

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.

paxdiablo
A: 

Do you really have to capture the line separators? If not, this regex should be all you need:

/\w+/

That's assuming all the substrings you want to match consist entirely of word characters, like in your example.

Alan Moore
+1  A: 

Maybe try $ (EOL/EOF) instead of (\r\n|\n)?

/\"(.+?)\".+?(\w.+?)$/
Marc Gravell