Stephane Maarek
Stephane Maarek

Reputation: 5352

Scala Regex Unclosed character error

I am trying to define this regex to detect the tag in my xml file

I have written the following code:

val regex = """<!DOCTYPE[^>[]*(\[[^]]*\])?>""".r

but it gives me the following error

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 27
<!DOCTYPE[^>[]*(\[[^]]*\])?>
                           ^

What am I doing wrong?

Upvotes: 0

Views: 411

Answers (2)

Maroun
Maroun

Reputation: 95968

<!DOCTYPE[^>[]*(\[[^]]*\])?>
            ↑       ↑   

You should escape the ] and [ inside the sets:

<!DOCTYPE[^>\[]*(\[[^\]]*\])?>

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174716

Escape the ], [ present inside the character class.

val regex = """<!DOCTYPE[^>\[]*(\[[^\]]*\])?>""".r

Upvotes: 1

Related Questions