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!

how to search perl string with special characters?

Status
Not open for further replies.

michael12

Programmer
Sep 26, 2002
25
0
0
US
I can use /[?%.]/ to search the string which contains those character, but how I can search some characters such as "/", "$" etc..., anyone can help me?
 
Escape them:

$string = '$100 dollars';

print "matched" if $string =~ /\$/; # -> matched

The backslash before a character tells Perl that the character following the backslash is to be interpreted:
1) as a special character, if it is otherwise normal
2) as a normal character (literal), if it is otherwise special.

If you are matching a bunch of funny characters, use the \Q and \E patter characters like so:

$string =~ /\Q$funny.characters-*&here\E/;

And the funny characters in between the \Q and the \E will be matched literally.

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top