|
DraxDrax Apprentice
Joined: 22 Mar 2009 Posts: 149
|
Posted: Thu Oct 28, 2010 6:17 am
[3.31] %subregex() parsing functions when replacing strings |
The Cmud help files for %subregex() give this example:
Cmud help file wrote: |
#SHOW %subregex("100 gold, 200 silver", "(\d+)", "%eval(\1+10)")
displays: 110 gold, 210 silver
The quotes "" around the %eval function are needed to prevent %eval from being executed immediately when %subregex is parsed.
With the quotes, it is executed each time a string is replaced. |
This shows the function %eval() being parsed inside of a %subregex() call each time a pattern is matched. Is %eval() the only function which can be used this way? I am trying to do something similar, but with the %db() function but I can't get it to parse properly.
Here's the general idea of what I am trying to accomplish in an alias 'foo':
Code: |
<alias name="foo">
<value><![CDATA[// Declare source of data key = val
$source.j = John
$source.p = Paul
$source.g = George
$source.r = Ringo
// Declare string to modify
$string = "&j sang Imagine. &p sang Yesterday. &g's guitar gently wept. &r just played the drums"
// Desired output is "John sang Imagine. Paul sang Yesterday. George's guitar gently wept. Ringo just played the drums"
// This does not work
$string = %subregex( $string, "&([jpgr])", "%db($source, \1)")
#SAY $string]]></value>
</alias> |
|
|
|
|
Anaristos Sorcerer
Joined: 17 Jul 2007 Posts: 821 Location: California
|
Posted: Thu Oct 28, 2010 7:37 am |
The problem is with $source. It's a local variable and, therfore, it must start with a $. So you must escape the symbol:
Code: |
<alias name="foo" id="547">
<value><![CDATA[// Declare source of data key = val
$source.j = John
$source.p = Paul
$source.g = George
$source.r = Ringo
// Declare string to modify
$string = "&j sang Imagine. &p sang Yesterday. &g's guitar gently wept. &r just played the drums"
// Desired output is "John sang Imagine. Paul sang Yesterday. George's guitar gently wept. Ringo just played the drums"
// This works!
$string = %subregex( $string, "&([jpgr])", "%db(~$source, \1)")
#SAY $string]]></value>
</alias>
|
Personally, I would have done it this way:
Code: |
<alias name="foo" id="547">
<value><![CDATA[// Declare source of data key = val
$source.j = John
$source.p = Paul
$source.g = George
$source.r = Ringo
// Declare string to modify
$string = "&j sang Imagine. &p sang Yesterday. &g's guitar gently wept. &r just played the drums"
// Desired output is "John sang Imagine. Paul sang Yesterday. George's guitar gently wept. Ringo just played the drums"
// This also works!
$string = %subregex( $string, "&(\w)", "%db(~$source, \1)")
#SAY $string]]></value>
</alias>
|
This would make the pattern key-independent (as long as the key was one character long) and it is faster because it doesn't have to check to see if it has the right value, any alphabetic character will successfully match.
Output:
Quote: |
John sang Imagine. Paul sang Yesterday. George's guitar gently wept. Ringo just played the drums
|
At any rate, the key factor is that you must change $source to ~$source in the function parameter.
The reason for the problem? $ is a reserved character. |
|
_________________ Sic itur ad astra. |
|
|
|
DraxDrax Apprentice
Joined: 22 Mar 2009 Posts: 149
|
Posted: Thu Oct 28, 2010 8:03 am |
Worked like a charm, Anaristos. Thanks!
I'm actually using a much more complicated regex, and much more convoluted code. What I posted was just a snippet to illustrate the issue I was having. |
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|