Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How to print a substring using regex

by ikegami (Pope)
on Sep 29, 2006 at 04:10 UTC ( #575459=note: print w/replies, xml ) Need Help??


in reply to How to print a substring using regex

Two common patterns:

my ($substr) = $var =~ /(pattern)/; print $substr, "\n" if defined $substr;
if ($var =~ /(pattern)/) { print "$1\n"; }

Don't forget to put the parens around what you want to capture.

Update: Two common patterns for multiple matches:

my @substrs = $var =~ /(pattern)/g; print "$_\n" foreach @substrs;
while ($var =~ /(pattern)/g) { print "$1\n"; }

Replies are listed 'Best First'.
Re^2: How to print a substring using regex
by Anonymous Monk on Oct 01, 2006 at 03:44 UTC
    Thanks for the responses, it worked like a charm.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2021-06-24 16:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (130 votes). Check out past polls.

    Notices?