Functions within regular expressions

I

IanW

Is it possible to execute a function within a regular expression. For
example, say I have a string like this:

my $string = "Item 1 cost is: 2345.67\nItem 2 cost is: 5678.90\n";

Say I have a function ( called formatPrice() ) that processes a number, like
2345.67 in that string, and makes it look like this:

$2,345.67

So, in a regexp I want to do something like this:

$string =~ s/(Item \d+ cost is: )(.+?)\n/$1formatPrice($2)\n/g;

That doesn't work as it stands, but is this kind of thing possible?

Thanks
Ian
 
P

Peter Makholm

IanW said:
So, in a regexp I want to do something like this:

$string =~ s/(Item \d+ cost is: )(.+?)\n/$1formatPrice($2)\n/g;

That doesn't work as it stands, but is this kind of thing possible?

You should look at the /e modifier for you substitution. Read 'perldoc
perlop' the paragraph starting with

s/PATTERN/REPLACEMENT/egimosx

//Makholm
 
M

Michele Dondi

Is it possible to execute a function within a regular expression. For

If it is a question, then it should end with a question mark.
If it is a question, then the answer is "yes"...
$string =~ s/(Item \d+ cost is: )(.+?)\n/$1formatPrice($2)\n/g;

....but this has nothing to do with "functions *within* regexen".
That doesn't work as it stands, but is this kind of thing possible?

/e


Michele
 
I

IanW

You don't want to execute a function from your regular expression,
instead, what you want is to do some action in the replacement.

That's an important difference.

I would write it as:

s/(Item \d+ cost is: )(.+)\n/$1 . formatPrice ($2)/eg;

That works nicely. Thanks :)
or, if I were to write it next week, after the release of 5.10:

s/Item \d+ cost is: \K(?<price>.+)(?=\n)/formatPrice ($+ {price})/eg;

Oo, just read up on \K - very neat. Not sure I get the bit afterwards
though.. "<price>" is presumably a way of naming the bracket match
variables, but not sure what it's advantage is over this:

s/Item \d+ cost is: \K(.+?)(?=\n)/formatPrice($1)/eg;

Ian
 
T

Ted Zlatanov

I> Oo, just read up on \K - very neat. Not sure I get the bit afterwards
I> though.. "<price>" is presumably a way of naming the bracket match
I> variables, but not sure what it's advantage is over this:

I> s/Item \d+ cost is: \K(.+?)(?=\n)/formatPrice($1)/eg;

Look through the introduction Yves Orton wrote:

http://www.regex-engineer.org/slides/perl510_regex.html

The advantage is a) clarity, and b) no more $1 $2 $3 renumbering when
you add a new match in the middle...

Ted
 
M

Michele Dondi

Oo, just read up on \K - very neat. Not sure I get the bit afterwards
though.. "<price>" is presumably a way of naming the bracket match
variables, but not sure what it's advantage is over this:

s/Item \d+ cost is: \K(.+?)(?=\n)/formatPrice($1)/eg;

The advantage is that $+{price} sounds much like a price.


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,208
Messages
2,571,079
Members
47,682
Latest member
TrudiConna

Latest Threads

Top