rnaeye has asked for the wisdom of the Perl Monks concerning the following question:

My Regex-1 matches the string although string has a space between "builtin.c \". I do understan why Regex-2 will match and Regex-2 will not. But I do not get why Regex-1 is matching. Please see my commented code for detaisl of the question. Thank you for your wisdom!

#!/usr/bin/perl use warnings; use strict; use 5.010; my $string = 'SRC=array.c builtin.c \ missing.c msg.c'; #Regex-1 Following matches, but WHY? String has a single space between + "builtin.c \" $string =~ m/^\w+=[^\n\\]*\\/; #Regex-2 Following also matches $string =~ m/^\w+=[^\n\\]* \\/; #pay attention to single space between + * and \ #Regex-3 Following does NOT match $string =~ m/^\w+=[^\n\\]* \\/; #pay attention to double spaces betwe +en * and \ say $string;

Replies are listed 'Best First'.
Re: help with regex
by graff (Chancellor) on May 05, 2013 at 23:52 UTC
    Your "Regex 1" matches because the space that precedes the backslash is neither a newline character nor a backslash. The only difference between Regex 1 and Regex 2 is that the latter will only match if the backslash is preceded by space, whereas in Regex 1, the backslash can be preceded by anything other than a newline or another backslash.

      Thanks for the help. It's simple after you explained it.