3

I am parsing a .txt, line by line, with considering a target token. I use a regex processor engine.

I match each line against:

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

where token is a string. When:

token="6-7(3-7" 

it arises the following exception:

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

How can I solve this?

1
  • You never close the ( in 6-7(3-7. You probably want to escape it: 6-7\\(3-7 Commented May 15, 2015 at 1:45

2 Answers 2

8

You have special characters in your token.

Have a look at Pattern.quote():

public static String quote(String s)

Returns a literal pattern String for the specified String.

This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern.

Metacharacters or escape sequences in the input sequence will be given no special meaning.

This should do the trick for you:

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

No need for doing the string magic yourself! :-)

Sign up to request clarification or add additional context in comments.

Comments

1

You should make sure to escape special characters in any plain-text string you use to make regex patterns. Replace "(" with "\(", and similarly for bare backslashes (before any other steps), periods, and all other special characters, at least all those you expect to see in the input. (If it's arbitrary input from users, assume every character will be included.)

1 Comment

I think it is not a good idea to suggest this solution without a full list of characters to be replaced. Anyway, the accepted answer is the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.