Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex match space not in parenthesis, accolades or squared barackets

Tags:

regex

I'm trying to split a sentence by whitespace/space but I must exclude space located inside parenthesis (), accolades {} or squared brackets [].

ex string: [apples carrots] (car plane train) {food water} foo bar should result in an array containing:

  • [apples carrots]
  • (car plane train)
  • {food water}
  • foo
  • bar

Any ideas?

like image 379
Romeo Mihalcea Avatar asked Jan 13 '23 08:01

Romeo Mihalcea


2 Answers

Not splitting, but matching and trimming. Example is in JavaScript, you can try it out in browser console:

var a = '[apples carrots] (car plane train) {food water} foo bar';
a.match(/[a-zA-Z0-9\[\]\(\){}]+/g).map(function (s) { return s.replace(/[\[\]\(\)\{\}]/, ''); });
["apples", "carrots", "car", "plane", "train", "food", "water", "foo", "bar"]

Alternatively:

a.split(/\s+(?![^\[]*\]|[^(]*\)|[^\{]*})/)

Produces:

["[apples carrots]", "(car plane train)", "{food water}", "foo", "bar"]
like image 123
uKolka Avatar answered May 12 '23 12:05

uKolka


Split on whitespace followed by a positive look-ahead that checks if next bracket char (if any) is an open one (or end of input):

\s+(?=[^\])}]*([\[({]|$))
like image 42
Bohemian Avatar answered May 12 '23 11:05

Bohemian