Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match only commas not in parentheses?

Tags:

java

regex

I have a string that looks something like the following:

12,44,foo,bar,(23,45,200),6 

I'd like to create a regex that matches the commas, but only the commas that are not inside of parentheses (in the example above, all of the commas except for the two after 23 and 45). How would I do this (Java regular expressions, if that makes a difference)?

like image 653
Paul Wicks Avatar asked Jan 27 '12 07:01

Paul Wicks


People also ask

How do you match a comma in regex?

The 0-9 indicates characters 0 through 9, the comma , indicates comma, and the semicolon indicates a ; . The closing ] indicates the end of the character set. The plus + indicates that one or more of the "previous item" must be present.

How do you escape parentheses in regular expression?

Since parentheses are also used for capturing and non-capturing groups, we have to escape the opening parenthesis with a backslash. An explanation of how literalRegex works: / — Opens or begins regex. \( — Escapes a single opening parenthesis literal.

What is difference [] and () in regex?

This answer is not useful. Show activity on this post. [] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).


1 Answers

Assuming that there can be no nested parens (otherwise, you can't use a Java Regex for this task because recursive matching is not supported):

Pattern regex = Pattern.compile(     ",         # Match a comma\n" +     "(?!       # only if it's not followed by...\n" +     " [^(]*    #   any number of characters except opening parens\n" +     " \\)      #   followed by a closing parens\n" +     ")         # End of lookahead",      Pattern.COMMENTS); 

This regex uses a negative lookahead assertion to ensure that the next following parenthesis (if any) is not a closing parenthesis. Only then the comma is allowed to match.

like image 81
Tim Pietzcker Avatar answered Oct 05 '22 05:10

Tim Pietzcker