Escaping special characters in java regex (not quoting)
I'm trying to match user input with wildcards that are simpler than the java regex syntax. Say there's a wildcard A. The user would then enter the input string:
this ( is \ a $ test.
and match 'test' with the search string:
this ( is \ a $ %A%.
I do this by replacing the wildcard string in the search string with (.+?)
, so I can match the wildcard with the capturing groups of a normal regex. However, I still want the special characters to be escaped. If I use quote, the regex will no longer work, since the characters with regex meaning ((.+?)
) are also quoted:
String inputString = "this ( is \\ a $ test."
String searchString = "this ( is \\ a $ %A%."
St开发者_运维百科ring regex = Pattern.quote(searchString);
//regex = "\\Qthis ( is \\ a $ %A%.\\E"
regex = regex.replaceFirst("%A%", "(.+?)");
//regex = "\\Qthis ( is \\ a $ (.+?).\\E"
Matcher matcher = Pattern.compile(regex).matcher(inputString); //no match
Is there a built-in way to really escape special characters, not quote the entire string?
You need to find the %A%
, quote everything before it, everything after it, and replace it with the matching regex syntax.
I'm not sure what the full requirements are for this wildcard, but if it can only ever be %A%
it would look something like this:
String searchString = "this ( is \\ a $ %A%.";
String extractorToken = "(.+?)";
int indexOfWildcard = searchString.indexOf("%A%");
String pattern = Pattern.quote(searchString.substring(0, indexOfWildcard)) +
extractorToken +
Pattern.quote(searchString.substring(indexOfWildcard + 3, searchString.length()));
Matcher matcher = Pattern.compile(pattern).matcher(inputString);
If the wildcard can have different forms you might instead use a regex to locate the position of the wildcard and then do the above.
精彩评论