Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex and Math

Status
Not open for further replies.

Keithlol

IS-IT--Management
Joined
Sep 22, 2006
Messages
2
Location
US
Is it possible to replace all numbers in a string with a mathematical expression using each number found in the string?
In other words, I have this:
$adjust="1.5";
the string:
$line="The first board's width=\"450\", the second board's width="350", the third board's width=\"550\".";

What I need to do is replace every width number by that number multiplied by $adjust.
ie: (This will never work... ha)
$line =~ s/width=\"(\d+)\"/$_*$adjval/g;

Actually, it is an html page in an array that I would like to change every image width by a percentage. I can do it by spliting each scalar in the array then doing the substitution in a foreach loop. But I would like a one-liner or so to do it. The above is just for example.
Any ideas?
Thanks,
Keith

 
you were pretty close:

Code:
$adjust = 1.5;
$line = "The first board's width=\"450\", the second board's width=\"350\", the third board's width=\"550\".";
$line =~ s/width="(\d+)"/'width="'. $1*$adjust . '"'/eg;
print $line;
 
Kevin,
Thanks for your solution. Works like a CHARM!
You da man!
Now, why didn't I think of that???¿¿

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top