Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

regex needed

by Anonymous Monk
on Nov 18, 2008 at 13:26 UTC ( [id://724253]=perlquestion: print w/replies, xml ) Need Help??

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

If I'm trying to do an exact match but want to exclude the possibility of a greater than sign and/or spaces what would the regex look like.

My attempt grabs the first one on the list but should only be grabbing the 5th one on the list???

Heres my attempt:

while (<DATA>) { chomp; if (/^(?:[\>]$var$)/) { print "MATCH => $_\n"; } } __DATA__ >Sensor30 >FooSensor30 > Sensor30 Sensor300 Sensor30 >Sensor3

Replies are listed 'Best First'.
Re: regex needed
by jwkrahn (Abbot) on Nov 18, 2008 at 13:33 UTC

    Perhaps you want:

    while (<DATA>) { chomp; if ($var eq 'Sensor30') { print "MATCH => $_\n"; } }
Re: regex needed
by McDarren (Abbot) on Nov 18, 2008 at 13:37 UTC
    "If I'm trying to do an exact match..."
    Exact match for what?
    You didn't specify what $var contains.

    Anyway, does this do what you want?

    #!/usr/bin/perl -l use strict; use warnings; my $var = 'Sensor30'; while (<DATA>) { chomp; print if m/^\Q$var\E$/; } __DATA__ >Sensor30 >FooSensor30 > Sensor30 Sensor300 Sensor30 >Sensor3
    Cheers,
    Darren
Re: regex needed
by Bloodnok (Vicar) on Nov 18, 2008 at 14:04 UTC
    I'm not sure I understand the question - why would your attempt be only finding the 5th in the list - if you're attempting to preclude the possibility of a leading '>', then surely both the 4th & 5th elements of the list should be 'found' - unless of course the snippet is a little too small...

    Either way, suitable use of \s* would suffice, such that the snippet might become...

    while (<DATA>) { chomp; next if /^>\s*/; print "MATCH => $_\n"; } __DATA__ >Sensor30 >FooSensor30 > Sensor30 Sensor300 Sensor30 >Sensor3

    Note that inverting the sense of the makes the test for whitespace redundant since the next will be executed whenever the line begins with a '>'. To avoid any line whose first non-whitespace char, change /^>\s*/ to /^\s*>/.

    A user level that continues to overstate my experience :-))
Re: regex needed
by Anonymous Monk on Nov 18, 2008 at 13:44 UTC
    I forgot to mention that I need to exclude the ">" sign if it exists so in this case I would have two matches:
    >Sensor30 Sensor30

      Perhaps this one is, what you are looking for?

      C:\Daten\perl>cat match.pl #!/usr/bin/perl use strict; use warnings; my $var = 'Sensor30'; while (<DATA>) { print if /^>?\Q$var\E$/; } __DATA__ >Sensor30 >FooSensor30 > Sensor30 Sensor300 Sensor30 >Sensor3 C:\Daten\perl>perl match.pl >Sensor30 Sensor30

      Without a regular expression:

      C:\Daten\perl>cat match.pl #!/usr/bin/perl use strict; use warnings; my $var = 'Sensor30'; while (<DATA>) { chomp; print "$_\n" if $_ eq $var or $_ eq '>' . $var; } __DATA__ >Sensor30 >FooSensor30 > Sensor30 Sensor300 Sensor30 >Sensor3 C:\Daten\perl>perl match.pl >Sensor30 Sensor30
        Thats what I was missing...Thank you!

        I modified it a bit to include the space if it exists:

        my $var = 'Sensor30'; while (<DATA>) { chomp; if (/^(>\s*)?\Q$var\E$/) { print "MATCH => $_\n"; } } __DATA__ >Sensor30 >FooSensor30 > Sensor30 Sensor300 Sensor30 >Sensor3
Re: regex needed
by JavaFan (Canon) on Nov 18, 2008 at 19:54 UTC
    There are various ways to create a regex for this, but I wouldn't create such a one. I'd do (untested):
    while (<DATA>) { chomp; my $copy = $_; s/[< ]+//g; print "MATCH => $copy\n" if $_ eq 'Sensor30'; }
    But then, I assume you want to ignore greater than signs and spaces anywhere in the line, so you also want to match Se>>>nsor 30 and S e n s o r > 3 0.

Log In?
Username:
Password:

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

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

    No recent polls found