Alex Rivera | Logout

Regular expression to match EOF

Asked 2009-07-23T12:01:30.007
137

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?

Edit
Report

1 Answer

214

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).

Python:
\Z Match absolute string end

Java, C# (.NET), PHP, Perl:
\Z Match string end (before last newline if present)
\z Match absolute string end

Go:
\z Match absolute string end

All of the above:
\A Match absolute string start

answered 2009-07-23T12:03:57.053

Your Answer