简体   繁体   中英

How to escape text for regular expression in Java

Does Java have a built-in way to escape arbitrary text so that it can be included in a regular expression? For example, if my users enter "$5", I'd like to match that exactly rather than a "5" after the end of input.

Java 1.5 开始,是的<\/a>:

Pattern.quote("$5");

Difference between Pattern.quote<\/code><\/a> and Matcher.quoteReplacement<\/code><\/a> was not clear to me before I saw following example

s.replaceFirst(Pattern.quote("text to replace"), 
               Matcher.quoteReplacement("replacement text"));

响应可能为时已晚,但您也可以使用Pattern.LITERAL ,它会在格式化时忽略所有特殊字符:

Pattern.compile(textToFormat, Pattern.LITERAL);

I think what you're after is \\Q$5\\E . Also see Pattern.quote(s) introduced in Java5.

See Pattern javadoc for details.

First off, if

  • <\/li>
  • <\/li>
  • <\/li><\/ul>

    it won't put a 1 at the end. It will look at the search regex for the first matching group and sub THAT in. That's what $1, $2 or $3 means in the replacement text: matching groups from the search pattern.

    Indeed, this appears to be the default way to do i18n in Spring Framework. I put XML tags, as placeholders, into the strings and I use replaceAll() to replace the XML tags with the values at runtime.

    replaceAll() choked on it, with the following showing up in a stracktrace:

     The user could put in any kind of characters, including dollar signs, without issue. It behaved exactly the way you would expect.

    "

To have protected pattern you may replace all symbols with "\\\\\\\\", except digits and letters. And after that you can put in that protected pattern your special symbols to make this pattern working not like stupid quoted text, but really like a patten, but your own. Without user special symbols.

public class Test {
    public static void main(String[] args) {
        String str = "y z (111)";
        String p1 = "x x (111)";
        String p2 = ".* .* \\(111\\)";

        p1 = escapeRE(p1);

        p1 = p1.replace("x", ".*");

        System.out.println( p1 + "-->" + str.matches(p1) ); 
            //.*\ .*\ \(111\)-->true
        System.out.println( p2 + "-->" + str.matches(p2) ); 
            //.* .* \(111\)-->true
    }

    public static String escapeRE(String str) {
        //Pattern escaper = Pattern.compile("([^a-zA-z0-9])");
        //return escaper.matcher(str).replaceAll("\\\\$1");
        return str.replaceAll("([^a-zA-Z0-9])", "\\\\$1");
    }
}

Pattern.quote("blabla") works nicely.

It encloses the sentence with the characters " \\Q<\/strong> " and " \\E<\/strong> ", and if it does escape "\\Q" and "\\E". However, if you need to do a real regular expression escaping(or custom escaping), you can use this code:

String someText = "Some/s/wText*/,**";
System.out.println(someText.replaceAll("[-\\[\\]{}()*+?.,\\\\\\\\^$|#\\\\s]", "\\\\$0"));

^(Negation) symbol is used to match something that is not in the character group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM