aclowkay
aclowkay

Reputation: 3877

Scala split string by group x

I'm looking for a way to split a string in Scala by a regular expression with a group.

For example split by a dot which is NOT preceded by a backslash. I tried use

"[^\\\\]\\."r.split("a.b.c\.d.e)

But it included the previous string which was not a dot character.

Expected: a,b,c,c\.d,e 
Result: , , c\., e

Upvotes: 0

Views: 267

Answers (1)

Leo C
Leo C

Reputation: 22439

You can use Regex negative lookbehind to exclude matching any . with a preceding \ as follows":

val pattern = """(?<!\\)\.""".r

pattern.split("""a.b.c\.d.e""")

// res1: Array[String] = Array(a, b, c\.d, e)

Upvotes: 2

Related Questions