[wellylug] Perl regular expression problem with literal $
Cliff Pratt
enkidu at cliffp.com
Sun Jun 1 12:51:35 NZST 2008
Peter Davenport wrote:
> Not a direct Linux issue, but I do run this process under Linux.
>
> I have a perl script to process some data with many records of the form -
> 'User xxxxxx transfered 123456kb of file yyyyyy at cost of $1,234.56 to
> machine zzzzz'
>
> This example is a bit made up but it illustrates the point. I am trying
> to extract the numbers for the kb and $.
>
> My perl RE to get the kb is m/ (\d+)kb / and this works as it gets the
> leading/trailing blanks and the numeric kb item into $1 variable.
> However, for the money item, I tried m/ $([\d,.]+) / but the $ gets used
> to indicate the end of record. Escaping it with backslash dosn't work
> either as it then seems to get interpreted as the start of a
> scalar variable. How do I specify a literal $ in the RE?
>
> This data extraction requirement must be a common one and been solved
> before. I have not been able to locate a solution.
> Can anyone help to resolve this please.
>
It's not matching your RE. The RE should read:
/ \$([\d.]+ )/
Note, no comma.
Here's my test snippet:
#!/usr/bin/perl
use strict ;
use warnings ;
my $str = 'abcdef $1234.00 xyz' ;
print "$str\n" ;
$str =~ / \$([\d.]+ )/ ;
print "$1\n" ;
Cheers,
Cliff
More information about the wellylug
mailing list