java - Pattern Syntax Exception in regex constructed with inputted text -


i parsing .txt, line line, considering target token. use regex processor engine.

i match each line against:

"(^|.*[\\s])"+token+"([\\s].*|$)" 

where token string. when:

token="6-7(3-7"  

it arises following exception:

exception in thread "main" java.util.regex.patternsyntaxexception:  unclosed    group near index 27 (^|.*[\s])6-7(3-7([\s].*|$) 

how can solve this?

you have special characters in token.

have @ pattern.quote():

public static string quote(string s)

returns literal pattern string specified string.

this method produces string can used create pattern match string s if literal pattern.

metacharacters or escape sequences in input sequence given no special meaning.

this should trick you:

string pattern = "(^|.*[\\s])" + pattern.quote(token) + "([\\s].*|$)"; 

no need doing string magic yourself! :-)


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -