Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Searching for a specific pattern.

by Anonymous Monk
on Jun 18, 2010 at 19:15 UTC ( #845442=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I working on a code and I have to search for a specific letters code coming from a file, the values I am checking against are: XLX or TLX or XLT or LX.
I am doinf a simple check like this:
my $type="LX"; if( $type=~/LX/) { do stuff ... }

I know that this wouldn't work because all the other letter codes has the "LX" in them, that would fail, correct?
I am doing this way:
my $type="LX" if( $type=~/\bLX\b/) { do stuff ... }

Is this way the more reliable way to get "LX" by using word boundary or there is a better and more efficient way to do this?
Thanks for the help!

Comment on Searching for a specific pattern.
Select or Download Code
Re: Searching for a specific pattern.
by phenom (Chaplain) on Jun 18, 2010 at 19:18 UTC
    If you're looking for EXACTLY "LX", then just use 'eq':
    if( $type eq 'LX' ) {
      I can't use that, using XML::XPATH, I need to use regular expression for this.

        I don't see the relation between XPaths and regular expressions, but /^LX\z/ checks for exactly "LX".

        ( An XPath that checks the value of an attribute would look like /foo/bar[@type="LX"] )

Re: Searching for a specific pattern.
by almut (Canon) on Jun 18, 2010 at 19:25 UTC

    If you want to avoid that the "LX" etc. will match substrings of words, then yes, using word boundary markers is the way to go.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-09-23 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (217 votes), past polls