CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
a useful regular expression

 



bird42
New User

Oct 15, 2003, 6:44 PM

Post #1 of 8 (1265 views)
a useful regular expression Can't Post

 I want to substitute numbers with thier decremented value. That is, I want a 1 to become 0, a 2 to become 1, etc on up to 25 or so. Is this easy?


toshesh
Novice

Oct 15, 2003, 7:06 PM

Post #2 of 8 (1264 views)
Re: [bird42] a useful regular expression [In reply to] Can't Post

dont quite understand what you're trying to do but something like

$string =~ /(\d*)/;

my $val = $1--;


KevinR
Veteran


Oct 15, 2003, 10:08 PM

Post #3 of 8 (1261 views)
Re: [toshesh] a useful regular expression [In reply to] Can't Post

that will work for a single number of any number of digits, 2, 89, 3452, etc, but will not work for several numbers contained in the same string.

I can't think of a simple way using only a pattern match/substitution to do this for all digits/numbers 1 thru 25 in the same string. I might be possible, but I just can't think how.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Oct 15, 2003, 10:41 PM

Post #4 of 8 (1260 views)
Re: [bird42] a useful regular expression [In reply to] Can't Post


Code
s/(\d)/$1 - 1/ge;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


KevinR
Veteran


Oct 15, 2003, 11:26 PM

Post #5 of 8 (1259 views)
Re: [davorg] a useful regular expression [In reply to] Can't Post

yes but how is that going to only replace numbers 1 thru 25 with a decremented value?
-------------------------------------------------


KevinR
Veteran


Oct 15, 2003, 11:37 PM

Post #6 of 8 (1257 views)
Re: [davorg] a useful regular expression [In reply to] Can't Post

this seems to work OK for numbers 1 thru 25 only:


Code
 my $start = "my cat is 12 years old my dog is 24 my horse is 88";  

$start =~ s/(\d+)/dec($1)/ge;

sub dec {
$_ = shift;
$_-=1 if ($_ > 0 && $_ < 26);
return ($_);
}

print $start;

-------------------------------------------------


(This post was edited by KevinR on Oct 15, 2003, 11:38 PM)


toshesh
Novice

Oct 15, 2003, 11:39 PM

Post #7 of 8 (1255 views)
Re: [KevinR] a useful regular expression [In reply to] Can't Post

deosn't that depend on how the data is stored?

if it is in a single string there must be a separator of some sort.


KevinR
Veteran


Oct 15, 2003, 11:54 PM

Post #8 of 8 (1254 views)
Re: [toshesh] a useful regular expression [In reply to] Can't Post


In Reply To
deosn't that depend on how the data is stored?

if it is in a single string there must be a separator of some sort.



It might. If the string contains mixed data like: Dan23 then that will become Dan22.

That can be remedied like this:

$start =~ s/\b(\d+)\b/dec($1)/ge;

But if its words/numbers or just numbers separated by white space then I don't think there should be any problem.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives