mnish
mnish

Reputation: 3897

regex split text with delimiter but don't split when the delimiter is in between ()

I wanted to split a text in Scala with , delimiter but don't split when the , is in parentheses but extract the , and ignore the parentheses, for example to split the following:

one, two (,) three

I should get an array containing:

`one`
`two , three`

Thank you in advance

Upvotes: 1

Views: 168

Answers (2)

vks
vks

Reputation: 67968

(?![^)()]*\)),|\((?=,)|(?<=,)\)

You can try this. See demo: http://regex101.com/r/kM7rT8/2

You will have to join the last three contents of list to get the second match.

Upvotes: 1

anubhava
anubhava

Reputation: 784948

You can use this lookaround based regex:

(?<!\)), *(?!\))

In Java:

(?<!\\)), *(?!\\))

RegEx Demo

Code Demo

It will break example input into:

  • one
  • two (,) three

Upvotes: 1

Related Questions