86

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?

||||||
1

Recently I was looking for something like this, but for JavaScript.

Putting this here, so that anyone with the same issue can benefit

var matchEndOfInput = /$(?![\r\n])/gm;

Basically this would match the end of the line, which is not followed by carriage return or new line characters. In essence this is the same as \Z but for JavaScript.

||||||
16

In Visual Studio, you can find EOF like so: $(?![\r\n]). This works whether your line endings are CR, CRLF, or just LF.

As a bonus, you can ensure all your code files have a final newline marker like so:

               Find What: (?<![\r\n])$(?![\r\n])
            Replace With: \r\n
 Use Regular Expressions: checked
Look at these file types: *.cs, *.cshtml, *.js

How this works:

Find any line end (a zero-width match) that is not preceded by CR or LF, and is also not followed by CR or LF. Some thought will show you why this works!

Note that you should Replace With your desired line-ending character, be it CR, LF, or CRLF.

||||||
  • 1
    Fantastic! Thanks – MikeMurko Sep 14 '16 at 3:13
8

Contrast the behavior of Ryan's suggested \Z with \z:

$ perl -we 'my $corpus = "hello\n"; $corpus =~ s/\Z/world/g; print(":$corpus:\n")'
:helloworld
world:
$ perl -we 'my $corpus = "hello\n"; $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.

||||||
24

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.

||||||
155

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

||||||
  • 5
    Just a heads up if you are after such fonctionality in netbeans for a project files search as opposed to an in file search, the following will behave differently... (\s*)\?>(\s*)\Z ... and after some more digging here is what would work on a project folder: (\s*)\?>(\s*)(\n*)(\W)\Z FYI: this is to replace all closing php tags by line breaks at end of file. – MediaVince Aug 7 '14 at 9:32
  • 1
    Turns out \A also works in Visual Studio find and replace. As always use such things with caution but it saved me a ton of manual messing about once I was happy it would actually do the right thing. – Steve Pettifer Oct 19 '16 at 13:09
  • While I am using Java's Scanner class to read an entire file at once; if I use \Z as delimiter, trailing newline character trimmed. When I changed delimiter to \z, trailing newline character preserved. It seems that Martin Dorey's answer also applies to Java. – mmdemirbas Mar 6 '18 at 13:07
2

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

/\"(.+?)\".+?(\w.+?)$/
||||||
3

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.

||||||
0

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

||||||
  • 5
    Probably. I don't remember anymore :-) – cube Nov 21 '12 at 14:02
1

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|,|$)

||||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.