Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Regex failing when the line starts with "./"

by perl_mystery (Beadle)
on Feb 03, 2011 at 09:24 UTC ( [id://885933]=perlquestion: print w/replies, xml ) Need Help??

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

I have input lines that start with "./" and ".\",my regex works when the line starts with ".\" fails for the other one,I need help to write a regex that fits in both the scenarios?

INPUT:- .\amss\platform\cs\bin\cdep.exe-43041;FILE.flf;//deploy/core/cs/pk/rel +/1.4/bin/cdep.exe - VU_CORE_CS_PK.01.04.00.07.00 : 5 ./modem_proc/build/ms/dmss_flags.min-000197;FILE.flf;//depot/asic/buil +d/ms/dmss_flags.txt - BUILD_MS.00.00.02 : 5 I have the following code which fails when the line starts with "./",h +ow do I modify the below code to suit for both the above inputs? OUTPUT:- KEYS:-//deploy/core/cs/pk/rel/1.4/bin/cdep.exe //depot/asic/build/ms/dmss_flags.txt VALUES:- amss\platform\cs\bin\cdep.exe modem_proc/build/ms/dmss_flags.min my %hash_basebuild_rpf; open my $inputfh, '<', $basebuild_rpf_file or die "could not open $bas +ebuild_rpf_file - $!"; while (my $line = <$inputfh>) { chomp($line); my ($key) = $line =~ /;([^;]+)\s-\s/; print "KEY:$key\n"; my ($value) = $line =~ /\.\\(.*);/; # this line fails when the lin +e starts with "./" print "VALUE:$value\n"; $hash_basebuild_rpf{$key} = $value unless (exists $hash_basebuild_ +rpf{$key}); }

Replies are listed 'Best First'.
Re: Regex failing when the line starts with "./"
by davido (Cardinal) on Feb 03, 2011 at 09:38 UTC

    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

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

Re: Regex failing when the line starts with "./"
by tospo (Hermit) on Feb 03, 2011 at 09:40 UTC
    try
    my ($value) = $line =~ /\.[\\\/](.*);/;
      my ($value) = $line =~ /\.[\\\/](.*);/;

      That won't match lines starting with '/.'

      Update: ./ /. \. .\.... too many dots and slashes. I got it wrong.

      :)


      Dave

Re: Regex failing when the line starts with "./"
by Anonymous Monk on Feb 03, 2011 at 10:21 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://885933]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-04-24 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found