in reply to Regex failing when the line starts with "./"

Maybe the faulty line should read as follows:

my( $value ) = $line =~ m{(?:\.\\|\./)(.*);};

(Re updated to correct the order of dot and slash)

Match either '.\' or '/.' followed by everything up to and including ';'. Note, I used '{' and '}' curly brackets to improve legibility. It's bad enough with all those \'s and /'s. I also used non-capturing (?:....) brackets in constraining the alternation so that the capturing brackets used later on in the same RE would retain their position as $1.


Dave

Replies are listed 'Best First'.
Re^2: Regex failing when the line starts with "./"
by elef (Friar) on Feb 03, 2011 at 10:53 UTC
    I'm trying to wrap my head around this and it's not coming together.
    Wouldn't (?:\.\\|\./) mean "full stop, then either backslash or dot, then forward slash"?
    I have no time to test this now, but it seems to me that the correct regex would be:
    $line =~ m{(?:\.\\)|(?:\./)(.*);};
    with grouping on either side of the |. Or does a | placed within grouping parentheses always match the entire first/second half of the grouping?
      Wouldn't (?:\.\\|\./) mean "full stop, then either backslash or dot, then forward slash"? ... Or does a | placed within grouping parentheses always match the entire first/second half of the grouping?

      It is second case: [.\\] is one group of regex, [./] is the other.

      To match per your first query, regex would be qr{[.] (?: \\ | . ) /}x (which I would rather write as qr{[.] [.\\] /}x but that is besides the point).

        Carp! Please change ...

        [.\\] is one group of regex, [./] is the other

        ... to ...

        .\\ is one group of regex, ./ is the other