0

I have to read a file with some String values and transform them to Regex. So I am using the class Pattern of java.util.regex and I have some problem for some special characters.

For example if I have this String:

*Hello

when I am creating the regex with:

Pattern.compile("*Hello").matcher("*Hello").matches()

the result is false.

So I need to find a way to transform automatically the String in the compile in \\*Hello. How is it possible? Thanks in advice.

  • Are you looking for the `Pattern.quote` method, perhaps? – Jon Skeet Nov 17 '16 at 23:24
  • I don't think is the same: quote doesn’t escape the string itself, but wraps it. –  Nov 17 '16 at 23:32
  • Not sure what you mean by "wrap" here, but it does achieve what I thought your aim was - `Pattern.compile(Pattern.quote("*Hello")).matcher("*Hello").matches()` returns true. Does it matter whether that's via adding backslashes or a different means? – Jon Skeet Nov 17 '16 at 23:39

0 Answers0