Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split string by ',' unless ',' is within brackets using Regex?

Tags:

string

regex

EDIT: Can anyone help me out with a regular expression for a string such as this?:

[Header 1], [Head,er 2], Header 3

so that I can split this into chunks like:

[Header 1]
[Head,er 2]
Header 3

I have gotten as far as this:

(?<=,|^).*?(?=,|$)

Which will give me:

[Header 1]
[Head
,er 2]
Header 3

like image 932
Nate Avatar asked Apr 08 '09 21:04

Nate


4 Answers

In this case it's easier to split on the delimiters (commas) than to match the tokens (or chunks). Identifying the commas that are delimiters takes a relatively simple lookahead:

,(?=[^\]]*(?:\[|$))

Each time you find a comma, you do a lookahead for one of three things. If you find a closing square bracket first, the comma is inside a pair of brackets, so it's not a delimiter. If you find an opening bracket or the end of the line/string, it's a delimiter.

like image 156
Alan Moore Avatar answered Oct 26 '22 05:10

Alan Moore


\[.*?\]

Forget the commas, you don't care about them. :)

like image 20
JP Alioto Avatar answered Oct 26 '22 03:10

JP Alioto


Variations of this question have been discussed before.

For instance:

  • Regex to replace all \n in a String, but no those inside [code] [/code] tag

Short answer: Regular Expressions are probably not the right tool for this. Write a proper parser. A FSM implementation is easy.

like image 27
dmckee --- ex-moderator kitten Avatar answered Oct 26 '22 04:10

dmckee --- ex-moderator kitten


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

use the [ and ] delimiters to your advantage

like image 20
rampion Avatar answered Oct 26 '22 03:10

rampion