javascript - Regex - dealing with parentheses - Stack Overflow

admin2025-03-20  4

I'm getting better at regex, but I'm still having some trouble... I'm trying to extract a value from inside of a set of parentheses using regex. The problem I am having is when this set is preceded by an open parenthesis.

Here are some examples:

'x(1)'.match(/\((.+?)\)/)[1] // returns "1" as expected
'\)(1)'.match(/\((.+?)\)/)[1] // returns "1" as expected

The character before parentheses can be anything, including another open parenthesis

'\((1)'.match(/\((.+?)\)/)[1] // returns "(1", but I only want "1"

This is only a snippet of the string, so this portion is already split out.

Any help would be appreciated!

I'm getting better at regex, but I'm still having some trouble... I'm trying to extract a value from inside of a set of parentheses using regex. The problem I am having is when this set is preceded by an open parenthesis.

Here are some examples:

'x(1)'.match(/\((.+?)\)/)[1] // returns "1" as expected
'\)(1)'.match(/\((.+?)\)/)[1] // returns "1" as expected

The character before parentheses can be anything, including another open parenthesis

'\((1)'.match(/\((.+?)\)/)[1] // returns "(1", but I only want "1"

This is only a snippet of the string, so this portion is already split out.

Any help would be appreciated!

Share Improve this question asked Apr 8, 2011 at 15:54 MottieMottie 86.5k30 gold badges130 silver badges248 bronze badges 1
  • 1 Never use the dot when there is a more precise expression: [^()] – ridgerunner Commented Apr 8, 2011 at 16:11
Add a ment  | 

2 Answers 2

Reset to default 6

Never use the dot when there is a more precise expression. In this case you are looking to match the contents of an innermost parentheses. The contents are thus, any characters which are not open or closing parentheses like so:

/\(([^()]+)\)/

Based on that data set, the easiest thing to do would probably just be to add a quantifier to the open paren, eg:

/\(+(.+?)\)/

This way it matches any consecutive number of open parens leading up to what you want to capture, but must at least have one paren immediately preceeding the next part of the match. Example on rubular here. Note though: As I allude to below, this will capture open parens within the grouping that are delimited by other characters (ridgerider provides an example with ( (stuff)) in the ments below, similar would be something like (1(1). It is not explicitly clear from your sample if this is desired behavior, but if not then see below.

Operating under the assumption that you aren't going to need to capture a ( anywhere (i.e. you only want the innermost matching parens), you can explicitly make sure you don't match the ( character anywhere in your group if you use [^(]+? instead of .+?. It's certainly a more strict alternative, though. You can see that one on rubular here. To reiterate, though: this wouldn't allow the ( character anywhere within the group, which may or may not be valid in other data you're working with beyond this sample. You could also further refine this to [^()]+. The full regex would then be either /\(([^(]+?)\)/ or /\(([^()]+)\)/ (I believe these are semantically equivalent, but I probably haven't covered all corner cases, the latter is the "safest" by means of being the most strict).

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1742415616a212642.html

最新回复(0)