Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

help in matching

by uva (Sexton)
on Feb 08, 2006 at 09:51 UTC ( [id://528810]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

$_='process1'; if (/-process/) { print "success"; }
============================================================ in the above code what does the hypen resembles.....i read that is range operator for regular expression...i tried but i could not...please help me.

Replies are listed 'Best First'.
Re: help in matching
by Fletch (Bishop) on Feb 08, 2006 at 10:07 UTC

    The '-' character is for ranges, but only inside a character class. /[a-d]/ would match one single character 'a', 'b', 'c', or 'd'. /a-d/ matches an 'a' followed by a '-' followed by a 'd'. Your regex would match a dash followed by 'process' (i.e. '-process'); since your sample data does not contain a dash followed by 'process' the match fails.

Re: help in matching
by mickeyn (Priest) on Feb 08, 2006 at 10:13 UTC
    in this case the RE just looks for the character '-' which does not precede 'process'.
    to be used as range operator it should be placed within a character class.

    Enjoy, Mickey

Re: help in matching
by svenXY (Deacon) on Feb 08, 2006 at 10:15 UTC
    Hi,
    range operator here means that e.g. [a-z] means one character in the range of a-z.
    If you want your example to match, just omit the hyphen and it will match.

    It is however not a good idea to set $_ as it is a magic variable ( see perlvar).
    Regards,
    svenXY
Re: help in matching
by MCS (Monk) on Feb 08, 2006 at 12:01 UTC

    What you are currently doing is searching for "-process" in this $_ (magic operator) which you have assigned the value 'process1.' Running this yields nothing because '-process' is not in 'process1.' A better approach would be:

    my $string = 'process1'; if ($string =~ /process/) { print "success"; }

    This searches $string for the text "process." (for more information on these matches read up on regular expressions)

      thanks for your replies friends..... i understood..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://528810]
Approved by Corion
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.