CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Regular Expressions:
Regex from external app


New User

Jan 23, 2003, 2:46 AM

Post #1 of 5 (11045 views)
Regex from external app Can't Post

Trying some oddball stuff... may be impossible from what I see.
I want to have an external app fab a regex such as

and call Perl via a DOS type call such as
perl.exe "(stuff)" "X$1X" "g"

passing that text (the regex pieces) to my perl script via Argument SHIFT in from the command line that called my perl script. Perl will do whatever I want and fab a results.txt file for the external app to read.
It worked OK for simple regex's, but failed horribly when I tried the more complex regexes with () and $1 type stuff.

If I do a PRINT of what I shift in... it LOOKS like it is what I expect... but when I try to USE it via something like...
$find = shift; #(stuff)
$replace = shift; #X$1X >>> XstuffX
$rhs = shift; #g
$data = "this stuff does not work, stuff it";
$data =~ s/$find/$replace/$rhs;

The $rhs causes crash error message...
Scalar found where operator expected.

But even if I manually set it to "g", the $1 stuff causes problems. It is not evaluated to the () value and replaces as if it were plain text... X$1X instead of XstuffX.
The $find seems to be OK since I can manually set the replacement code to X$1X and it will work.
I could live without the $rhs, but the $1 logic is obviously important.

Can something like this be done?? By ANY method from the outside world?

Baffled... Mike H. thanks.


Jan 26, 2003, 11:16 PM

Post #2 of 5 (11032 views)
Re: [mfh721] Regex from external app [In reply to] Can't Post

That is because you can't have variables interpolate in the modifiers section. To do this, you might want to take a look at eval or interpolate modifers with (?-)

my $mod = 'i'; 
my $text = "H";
my $string = "hello";
print "It worked!" if $string =~ /(?$mod)\Q$text/;

New User

Apr 22, 2004, 11:41 AM

Post #3 of 5 (10908 views)
Re: [jryan] Regex from external app [In reply to] Can't Post

True, but that's not the main problem (and not the problem I'm having right now ;).

How do you get $1 to be interpreted on the substitution side when the whole substitution comes out of a string?



$text="The quick brown fox";
$pat='quick (.*) fox';
$rep='quick fox, who is $1';

($direct = $text) =~ s/quick (.*) fox/quick fox, who is $1/;
($indirect = $text) =~ s/$pat/$rep/;

print "Direct: " . $direct . "\nIndirect: " . $indirect . "\n";


Direct: The quick fox, who is brown
Indirect: The quick fox, who is $1

Can anybody help with that?




Apr 25, 2004, 1:28 PM

Post #4 of 5 (10897 views)
Re: [DReiners] Regex from external app [In reply to] Can't Post

For your case, use the /e modifier:

my ($indirect) = $text =~ s/$pat/$rep/e;

You may also want to read this:

New User

Apr 26, 2004, 9:16 AM

Post #5 of 5 (10894 views)
Re: [jryan] Regex from external app [In reply to] Can't Post

Ahh! That did the trick!

Thanks a lot



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives