How to clear a string for regular expression

I have java.lang.String that can contain any character, is there an easy way to misinform the string (for example, using an extra class that can do this?) And remove all the β€œdangerous” characters for later java.util.regex.Matcher or regular expression processing (e.g. $, ^, ...)?

+4
source share
2 answers

you can use

 Pattern.quote(string); 

to avoid your line for regex.

+15
source

You can also use the escape sequence \Q..\E

Here is an example of Metacharacters Inside character classes .

Any thing inside the pair \Q..\E is considered a literal.

+5
source

Source: https://habr.com/ru/post/1488438/


All Articles