[Brisbane-pm] Still Struggling With Regex Syntax
David Bussenschutt
David.Bussenschutt at qmtechnologies.com
Sun Apr 15 21:49:52 PDT 2007
Martin,
How about this:
# this example contains numbers, whitespace and other irrelevant stuff, possibly newlines
my $rain = " 345678 xxx\nblah";
#magic:
$rain =~ s/^.*?(\d+).*$/$1/s;
# my explanation:
# the basic substitution operator looks like this: s///; but with extra characters between the slashes as required below....
# ^.*? means match anything, starting from the start of the string (non-greedy, so it doesn't ever match a number)
# (\d+) means match any continual section of numbers, and the brackets means remember them as $1
# .*$ means match anything, finishing at the end string
# the $1 means "replace the entire matched string with just the bit remembered in $1" (ie the numbers).
# the 's' at the end means don't treat newlines as special ( http://perldoc.perl.org/perlre.html )
#NOTE: this code gives you left-most string of consecutive numbers in any string. eg if $rain started as 'xxx1234yyy5678' it would end up as just '1234'
print $rain; # prints the string '345678'
-----Original Message-----
From: brisbane-pm-bounces+david.bussenschutt=qmtechnologies.com at pm.org [mailto:brisbane-pm-bounces+david.bussenschutt=qmtechnologies.com at pm.org]On Behalf Of Martin Jacobs
Sent: Monday, 16 April 2007 2:34 PM
To: Brisbane Perl Group
Subject: [Brisbane-pm] Still Struggling With Regex Syntax
Hi folks,
I still don't get it (quite).
The story so far...
I have split my rainfall record, and the right-most scalar is $rain.
I now want to make sure that $rain holds a numeric value, without any whitespace, newlines or other nasties.
If I try
$rain = ($rain =~ m|\d+\.?\d*|);
It returns 1, which, presumably, is the number of times it gets a match.
What's the right syntax for saying 'make $rain contain the numeric values in $rain"?
Regards,
Martin
Visit my website...
<http://web.mac.com/martin_jacobs1> http://web.mac.com/martin_jacobs1
The message and any attachment is confidential and may be privileged or otherwise protected from disclosure. If you have received it by mistake please let us know by reply and then delete it from your system; you should not copy the message or disclose its contents to anyone.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/brisbane-pm/attachments/20070416/e93f492b/attachment.html
More information about the Brisbane-pm
mailing list