Reputation: 31546
If I have a string like
foo&bar&baz
and I want to extract tokens foo, bar, baz from it. a regex is easy to write
val regex = "([^&]+)".r
regex.findAllIn("foo&bar&baz").map(_.toString).toList
This gives me the answer I want.
List("foo", "bar", "baz")
But the input can have the & symbol escaped with _&_
So if the input is
foo_&_bar&baz
The output should be foo&bar, baz.
I googled and found this thread which has similar problem
RegEx disallow a character unless escaped
Based on this thread I changed my regex to
val regex = "((?:_&_|[^&]*)+)".r
But this doesn't work the output is
List("foo_", "", "_bar", "", "baz", "")
Upvotes: 1
Views: 56
Reputation: 626926
You may use
val regex = "(?:_&_|[^&])+".r
println( regex.findAllIn("foo_&_bar&baz").map(_.toString).toList )
// => List(foo_&_bar, baz)
See the regex demo and a Scala demo.
The (?:_&_|[^&])+
regex matches 1 or more repetitons of _&_
or, if not found at the current location, char other than &
.
Upvotes: 2