Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Applying a regular expression to a string

by BazB (Priest)
on Apr 04, 2005 at 11:46 UTC ( #444630=note: print w/ replies, xml ) Need Help??


in reply to Applying a regular expression to a string

Try something like:

my $message_id = $1 if $message =~ m/([\w\d]+)\s+.*/;

If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
That way everyone learns.


Comment on Re: Applying a regular expression to a string
Download Code
Re^2: Applying a regular expression to a string
by Smylers (Pilgrim) on Apr 04, 2005 at 13:22 UTC
    my $message_id = $1 if $message =~ m/([\w\d]+)\s+.*/;

    Putting my (a compile-time directive) with an if statement modifier (which can only take place at run-time) is confusing, and therefore deprecated.

    Also, there's no point at all in having .* at the end of the regexp: since one of the things it would happily match is nothing, it isn't making any restrictions at all about what can follow the \s+, so is just adding noise.

    Finally, the \w class already includes digits, so writing [\w\d] doesn't add anything.

    Here's how I might have written this:

    $message =~ /^(\w+)/ or die "Message '$message' doesn't start with an identifier.\n"; my $message_id = $1;

    die might not be the most appropriate thing to do here; adjust as appropriate. But if you're adamant that $message could never not match the pattern, then a die test is better than nothing: if you're right, then it'll never happen, and if you're wrong then your assumption was wrong so there's a problem elsewhere and at least you found out about it.

    Smylers

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2015-07-05 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls