Dave's Brain

Browse - Programming Tips - How do I protect a string that is going to be used in a regular expression?

Date: 2012oct2
Language: perl, php, Java

Q.  How do I protect a string that is going to be used in a regular expression?

A.  In perl:

$ok_for_pattern = quotemeta($s);
In php:
$ok_for_pattern = quotemeta($s);
In Java:
String ok_for_pattern = Pattern.quote(s);

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.