$x = 'andrew john';
# swap the first and second word around
$x=~ s/ (\w+) \s+ (\w+*)
/ $2 $1
/ x;
# s/ - substitute
# (\w+) - 1+ word charcters, captures as $1
# \s+ - 1+ whitespace characters
# (\w+) - 1+ word chars, captured as $2
# /x - use whitesapce and comments
# $1 and $2 survive
print $2;
# prints john
Regex : Capture Matched text
Use brackets to capture text, and $1, $2 ... to refer to it
Labels:
perl regex regular expressions
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment