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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matching a string with a $ in it

Status
Not open for further replies.

tjlst15

IS-IT--Management
Mar 20, 2002
15
0
0
US
How do I match the folloing string in a line of text? I have to use the $team variable, so I can't escape the $ signs.

Example:


$team = '$moneymaker$';
next if ( $string !~ /$team/ );

It never matches because it evaluates the $moneymaker in the $team string. How do I avoid that?
 
Whenever dealing with literals within regular expressions, take advantage of the quotemeta escape sequence to escape any special characters while still allowing for the interpolation of variables.

Code:
[blue]$team[/blue] = [red]'[/red][purple]$moneymaker$[/purple][red]'[/red][red];[/red]
[olive][b]next[/b][/olive] [olive][b]if[/b][/olive] [red]([/red] [blue]$string[/blue] !~ [red]/[/red][purple][purple][b]\Q[/b][/purple][blue]$team[/blue][purple][b]\E[/b][/purple][/purple][red]/[/red] [red])[/red][red];[/red]

is equivalent to:

Code:
[olive][b]next[/b][/olive] [olive][b]if[/b][/olive] [red]([/red] [blue]$string[/blue] !~ [red]/[/red][purple][purple][b]\$[/b][/purple]moneymaker[purple][b]\$[/b][/purple][/purple][red]/[/red] [red])[/red][red];[/red]
- Miller
 
try using the \Q..\E modifiers:

Code:
$team = '$moneymaker$';
next if ( $string !~ /\Q$team\E/ );

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
see, Miller agrees with me [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Nice, that worked perfectly. Thanks Guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top