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

Painfully basic regexp question

Status
Not open for further replies.

dcnguyen

Technical User
Mar 22, 2005
54
US
After reading through a few tutorials, I'm convinced I have a mental block when it comes to regexps. All I'm trying to do is find out if a string contains the characters %,$,!,@,<,>

How would I do this? According to some guides I read, special characters in brackets (with the exception of a few) don't need to be escaped. But this doesn't appear to work:

[%$!@<>]

 
The $ -DOES- need to be escaped in the class, otherwise it is evaluated/interpolated (choose your language).

So a regex of /[%\$!@<>]/ would work:

Code:
[olive][b]for[/b][/olive] [red]([/red][red]'[/red][purple]Clean[/purple][red]'[/red],[red]'[/red][purple]per%cent[/purple][red]'[/red],[red]'[/red][purple]dol$lar[/purple][red]'[/red],[red]'[/red][purple]Excla!mation[/purple][red]'[/red],[red]'[/red][purple]a@t[/purple][red]'[/red],[red]'[/red][purple]less<than[/purple][red]'[/red],[red]'[/red][purple]greater>than[/purple][red]'[/red],[red]'[/red][purple]Clean[/purple][red]'[/red][red])[/red] [red]{[/red]
  [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$_[/blue] Contains one of our characters[purple][b]\n[/b][/purple][/purple][red]"[/red] [olive][b]if[/b][/olive][red]([/red][red]/[/red][purple][%[purple][b]\$[/b][/purple]!@<>][/purple][red]/[/red][red])[/red][red];[/red]
[red]}[/red]
 
According to some guides I read, special characters in brackets (with the exception of a few)

You can always escape them all if unsure and it will not hurt anything:

[\Q%$!@<>\E]

\Q escapes everything until \E

where more people possibly get confused is thinking meta charaters are interpolated inside a character class. They try something like:

[.]*

and then don't understand why it's not matching everything.







------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks all. I thought I understood the concept but looks like I got hung up on the exceptions
 
Y'all are both somewhat correct.

Basically, the problem here is that regular expressions are interpolated unless you use the single quote delimiter. This means that all $ and @ should be escaped unless you know for sure that it is not possible for them to create a variable name. And just to be on the safe side, you might as well always escape them. This is even true in Kevin's use of the quotemeta escape sequence \Q...\E.

Code:
for ('Clean','per%cent','dol$lar','Excla!mation','a@t','less<than','greater>than','Clean') {
  print "$_ matches while escaped\n" if /[%[b]\[/b]$![b]\[/b]@<>]/;
  print "$_ matches while not interpolated\n" if m'[%$!@<>]';
}

It happens that bigmar's regex will work, but if the @ sign happened to be followed by a legal variable name character, then that would have been interpolated just like $! will without escaping.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top