Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem executing the right side of s///e regexp

Status
Not open for further replies.

tanderso

IS-IT--Management
Aug 9, 2000
981
US
This works:

$content =~ s/(href\s*=\s*["'])($base(.*?))(["'])/
$1http:\/\/$ENV{'SERVER_NAME'}$ENV{'SCRIPT_NAME'}
\?URL=$2&HEADER=$header$4
/gis;

This doesn't:

$content =~ s/(href\s*=\s*["'])($base(.*?))(["'])/
$1http:\/\/$ENV{'SERVER_NAME'}$ENV{'SCRIPT_NAME'}
\?URL=&url_encode($2)&HEADER=$header$4
/egis;

I need to run url_encode() on one of the matches, but I get a "bareword where operator expected near '$1http'". I'm guessing that using the /e switch requires the entire right side to be a statement? I've checked the docs and can neither confirm nor disconfirm this hypothesis. If that is the case, then how can I do a replace with both string components and function components like I want?
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
If you are breaking the s/// syntax across multiple lines, you need an 'x' switch to allow the use of 'extended regex's'.

$content =~ s/(href\s*=\s*["'])($base(.*?))(["'])/
$1http:\/\/$ENV{'SERVER_NAME'}$ENV{'SCRIPT_NAME'}
\?URL=$2&HEADER=$header$4/egis[red]x[/red];


If that is not the problem, then, on occassions when it starts to get messy on the right side, I tend to put the replacement code in a sub and call the sub in the right side.

$content =~ s/pattern/&generate_replacement/egis;

sub generate_replacement
{
# do what ever and return() the desired string
}

' not as concise, but it works....




keep the rudder amid ship and beware the odd typo
 
Thanks. The /x didn't do anything. But, I tried putting it in a seperate subroutine and that worked. Then, that got me thinking and I came up with the solution:

$content = s/match/"string".function()."string"/egis

This works since the right side is now an expression of string concatenations.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top