whysoserious
whysoserious

Reputation: 728

Scala Regex. Strange regular expression

Please have a look at the REPL session below. The only difference between both regexes is order of characters in square brackets. Thus, both should yield the same output. For some reason second regex omits first sequence (123). What's happening here? :)

Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.

scala> """[^+-.,><\[\]]+""".r.findAllIn("123]asdf") foreach { println }
123
asdf

scala> """[^+-><\[\].,]+""".r.findAllIn("123]asdf") foreach { println }
asdf

Upvotes: 2

Views: 118

Answers (1)

detunized
detunized

Reputation: 15289

- inside square brackets should be escaped as well. It's used to specify ranges like [0-9] to match all digits, for example.

Upvotes: 4

Related Questions