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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression subsitute 2

Status
Not open for further replies.

serializer

Programmer
May 15, 2006
143
SE
Hi,

I need some help with a regular expression.

I want to replace everything within a string except numbers and periods. In my example I am having a version string that looks like this:

"3.5.4 (en-US)"

I only want the remaining part to be:

"3.5.4"

Thank you for your help!
 
Hi,

This should work:

Code:
my $str = "3.5.4 (en-US)";

$str =~ s/[^\d\.]//g;


We're effectively replacing anything that doesn't either match a digit or period

Thanks,

Steven Parker
 
Hi every body!

I have a quesion for my problem with using mysql and perl programming.

- data from mysql is logic expression. After query I get it's value on string format:

my $data = "$sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/";

In this expression $sex,$age,$optid is variables

- How to make Perl understanding this function?
my $sex,$age,$optid;
if ($data) {
........
........
}

as

if ($sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/) {
........
........
}

Please!!!!
 
Try "eval"

Also, when you have a question that is unrelated to the current thread it would be wise to start a new thread rather than just tack onto the end of an existing and unrelated thread.



Trojan.
 
TrojanWarBlade - thank for your post

but can you expand your solution please?
 
if (eval($data)) {

Code:
$ perl
my $a = 5;
my $b = 10;
my $expr = '$a < 100 && $b > 0';
if (eval($expr)) {
	print "test 1 passed\n";
}
$expr = '$a > 100 && $b < 0';
if (eval($expr)) {
	print "test 2 passed\n";
}
__END__
test 1 passed

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
this problem is solved. Thank TrojanWarBlade, kirsle. Thank every body.
 
thangddhd, you can thank Kirsle by giving him a star (see the link under each post).

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top