pip9ball has asked for the wisdom of the Perl Monks concerning the following question:
I am pretty new to perl and need some ideas on how to build a regular expression to perform some substitutions, if at all possible. The input to the regular expression would be a string consisting of mathematical operations with variables '$<name>'. Basically, anytime the '$' keyword is found, replace with $sym{'$name'). Is this possible with regular expressions or do I need a parser?
eg. $x + $y = 7
$x * ($x - $y) + 10
$x + $x1 + $x2 ... = n
substitute with
$sym{'$x'} + $sym{'$y'} = 7
$sym{'$x'} * ($sym{'$x'} - $sym{'$y'}) + 10
$sym{'$x'} + $sym{'$x1'} + $sym{'$x2'} ... = n
This regular expression works for 1 letter variables, however it doesn't handle multiple vars.
$ref =~ s/(\$\S)/\$sym->{'$1'}/g;
Thanks in advance, -P
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regular expression match and substitution
by ikegami (Patriarch) on Nov 09, 2004 at 18:19 UTC | |
by pip9ball (Acolyte) on Nov 09, 2004 at 18:56 UTC | |
by ww (Archbishop) on Nov 09, 2004 at 19:09 UTC | |
by ikegami (Patriarch) on Nov 09, 2004 at 19:20 UTC | |
by pip9ball (Acolyte) on Nov 09, 2004 at 19:42 UTC | |
by ikegami (Patriarch) on Nov 09, 2004 at 21:35 UTC | |
|
Re: Regular expression match and substitution
by TedPride (Priest) on Nov 09, 2004 at 22:17 UTC |