Reputation: 11
I'm a beginner to Perl and Regular Expression. I want to extract certain amount dollar after $. So I write the following code. However, I try to avoid the first $ sign, that is, I only want the information after word "My" in this case.
my $str = 'His house is small; only worth $90,000;My house is so big; it worth $179,000; ';
if ( $str =~ /\$([\d,]+)/) {
my $used = $1;
print "House Price: $used\n";
}
Any help would be appreciated.
Upvotes: 1
Views: 60
Reputation: 4127
my $str = 'His house is small; only worth $90,000;My house is so big; it worth $179,000; ';
if ( $str =~ /My.*?\$(\d+,\d+)?/) {
my $used = $1;
print "House Price: $used\n";
}
House Price: 179,000
Upvotes: 1
Reputation: 36100
My.*?\K\$\d+(,\d+)?
My
- find My
.*?
- match the least amount of characters until you get to a $
sign\K
- drop everything matched so far (so it's not included in the result)\$\d+(,\d+)?
- support either integers or float amount of $
sUpvotes: 1
Reputation: 67968
^.*\bMy\b(*SKIP)(*F)|\$([\d,]+)
You can use (*SKIP)(*F)
here.See demo.
https://regex101.com/r/fM9lY3/52
Upvotes: 1