Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a String based on regex

Tags:

java

regex

I have a string that needs to be split based on the occurrence of a ","(comma), but need to ignore any occurrence of it that comes within a pair of parentheses. For example, B2B,(A2C,AMM),(BNC,1NF),(106,A01),AAA,AX3 Should be split into

B2B,
(A2C,AMM),
(BNC,1NF),
(106,A01),
AAA,
AX3
like image 985
user1724501 Avatar asked Oct 06 '12 04:10

user1724501


People also ask

Can you use regex to split a string?

You do not only have to use literal strings for splitting strings into an array with the split method. You can use regex as breakpoints that match more characters for splitting a string.

What is regex in Split method?

split(String regex) method splits this string around matches of the given regular expression. This method works in the same way as invoking the method i.e split(String regex, int limit) with the given expression and a limit argument of zero. Therefore, trailing empty strings are not included in the resulting array.

How do you split a string with regular expressions in Python?

If you want to split a string that matches a regular expression (regex) instead of perfect match, use the split() of the re module. In re. split() , specify the regex pattern in the first parameter and the target character string in the second parameter.

How do you split a string?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


1 Answers

FOR NON NESTED

,(?![^\(]*\))

FOR NESTED(parenthesis inside parenthesis)

(?<!\([^\)]*),(?![^\(]*\))
like image 97
Anirudha Avatar answered Oct 03 '22 05:10

Anirudha