Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

regex problem

by mosh (Scribe)
on Jun 08, 2005 at 09:33 UTC ( [id://464611]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I have to locate from a large amount of lines, the lines that starts with "iff", the problem is that there're also lines begins with "if".
I'm interested only in the Regex that seperates the two kinds.

In the code below I have to get "didn't catch you !!"

my $line = "Iff bla blabla bla..."; if ($line =~ /^If?/) #if ($line =~ /^If{1}/) { print "catch you !!\n"; } else{print "didn't catch you !!\n";}
In the second part i have to get (with the same regex): "catch you !!"
my $line = "If bla blabla bla..."; if ($line =~ /^If?/) #if ($line =~ /^If{1}/) { print "catch you !!\n"; } else{print "didn't catch you !!\n";}
Thanks,

Mosh.

Replies are listed 'Best First'.
Re: regex problem
by robartes (Priest) on Jun 08, 2005 at 09:41 UTC

    You're probably looking for something along these lines:

    /^If[^f]/ # If followed by anything that is not f

    The regex /^If?/ matches both your cases because you're looking for I followed by 0 or more 1 f's, so even Ig would match. The /^If{1}/ also matches both because both strings start with If, i.e. I followed by exactly one f. The second string has an additional f, but that particular regex is not concerned about that.

    Update: rev_1318 is right, of course (see this node)

    CU
    Robartes-

      The regex /^If?/ matches both your cases because you're looking for I followed by 0 or more f's

      Actually, that's 0 or 1 f's, not 0 or more :) (but who's counting)

      Paul

      Thanks for the detailed explanation !

      Mosh.

Re: regex problem
by muntfish (Chaplain) on Jun 08, 2005 at 09:40 UTC

    See perldoc perlre.

    If I understand you correctly, you need to distinguish between If followed by f, and If followed by something other than f ?

    The following regex will only match If but not Iff:

    /^If[^f]/

    Apologies if I've got the wrong end of the stick here.


    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&
Re: regex problem
by inman (Curate) on Jun 08, 2005 at 10:33 UTC
    The regex needs to be anchored to the start of a line and followed by a word break. The conditional part also needs to account for lines that do not begin with 'If' at all.
    #! /usr/bin/perl -w use strict; use warnings; while (<DATA>) { if (/^If(f?)\b/) { if ($1) { print "found Iff\n"; } else { print "found If\n"; } } else { print "no If\n"; } } __DATA__ If and only iff Iff this were the end the end last If Iffy code or what
Re: regex problem
by tlm (Prior) on Jun 08, 2005 at 12:19 UTC

    Another solution involves a negative lookahead assertion (see perlre):

    if ( /^if(?!f)/ ) { # begins with if (but not with iff) } else { # anything else }
    The (?!f) part imposes a zero-width condition on the match: it must not be followed by the character 'f'. The difference between this solution and /^if[^f]/ is that the latter regex fails on the string 'if'. In other words, with the negative lookahead, the match has length 2, whereas with a (negative) character class the match has length 3.

    the lowliest monk

      The above solution by tlm is clearly superior to that I'm about to suggest here.

      But another approach that I've used in environments (like Emacs) that don't have negative lookaheads is:

      /if([^f]|$)/
      or
      /if(?:[^f]|$)/
      to ensure nothing is returned in $1 should the regex match.
Re: regex problem
by tchatzi (Acolyte) on Jun 08, 2005 at 09:59 UTC
    It can be with a simple if/elsif and a simple regex, but i suggest you test what the others said. Take a deep breath and dive into the regex sea.
    open(F,"foo"); foreach(<F>){ if ($_ =~ /^iff/){ print "A line starts with iff\n"; }elsif($_ =~ /^if/){ print "A line starts with if\n"; } } close F;


    ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI
      The important thing with this approach is the order of the tests. Searching for the longest match first is the only way to be successful here, a bit like looking for a routing table match using binary arithmetic (the longest prefix match wins).
Re: regex problem
by murugu (Curate) on Jun 08, 2005 at 10:01 UTC

    Hi mosh,

    May be this regex help you to differentiate If and Iff.

    /^If\b/

    Regards,
    Murugesan Kandasamy.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-03-29 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found