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!

combining regexps

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
0
0
GB
Hi all,

I've got a set of regexps to escape asterisks and brackets:

Code:
$string =~ s/\*/\\\*/g;

$string =~ s/\(/\\\(/g;

$string =~ s/\)/\\\)/g;

I would use quotemeta, but I don't want to escape full stops (periods).

Can I combine the above statements into one regexp?

Is there a better way to escape a specific set of characters and not others?

Thanks, rotis23
 
$string = 'this is * a line of ( text with ) asterisks * and ( brackets ) in it';
print "before: $string\n";
$string =~ s/([*,(,)])/\\\1/g;
print " after: $string\n";


Kind Regards
Duncan
 
Also, why are you also backslashing the commas ","

in the substitution the [stuff in here] isnt comma separated, it's just a group of characters to match...I bet considering the fact rotis23 doesnt want to escape the periods that they are working on text stuff (like stuff people read/write) which would have "," in it...so just do this:

Code:
$string =~ s/([*()])/\\$1/g;

CGE
 
Thanks for your help peoples.

It's actually escaping bad email address characters before using them in an LDAP query.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top