Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match EOF

Tags:

regex

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?

like image 542
Ryan Avatar asked Jul 23 '09 12:07

Ryan


People also ask

What is the regular expression for end of line?

End of String or Line: $ The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If you use $ with the RegexOptions. Multiline option, the match can also occur at the end of a line.

How do you match in regex?

2.1 Matching a Single Character The fundamental building blocks of a regex are patterns that match a single character. Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" .

How do you match a space in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

How do I escape a character in regex?

The backslash character ( \ ) is the escaping character. It can be used to denote an escaped character, a string, literal, or one of the set of supported special characters. Use a double backslash ( \\ ) to denote an escaped string literal.


2 Answers

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 end

like image 193
Ryan Avatar answered Nov 12 '22 21:11

Ryan


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.

like image 39
paxdiablo Avatar answered Nov 12 '22 20:11

paxdiablo