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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



mfh721
New User

Jan 23, 2003, 2:46 AM

Post #1 of 5 (3673 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
s/(stuff)/X$1X/g

and call Perl via a DOS type call such as
perl.exe myscript.pl "(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.


jryan
User

Jan 26, 2003, 11:16 PM

Post #2 of 5 (3660 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 (?-)


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



DReiners
New User

Apr 22, 2004, 11:41 AM

Post #3 of 5 (3536 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?

Example:

#!/usr/bin/perl

$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";

Output:

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

Can anybody help with that?

Thanks

Dirk


jryan
User

Apr 25, 2004, 1:28 PM

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

For your case, use the /e modifier:


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


You may also want to read this: http://www.perldoc.com/perl5.8.0/pod/perlre.html


DReiners
New User

Apr 26, 2004, 9:16 AM

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

Ahh! That did the trick!

Thanks a lot

Dirk

 
 


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

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