I'm developing an algorithm to parse a number out of a series of short-ish strings. These strings are somewhat regular, but there's a few different general forms and several exceptions. I'm trying to build a set of regexes that will handle the various forms and exceptions; I'll apply them one after another to see if I get a match.
One of these forms goes something like this:
X (Y) Z
Where:
X
is a number I want to capture.
Z
is static, pre-defined text. it's basically how I determine whether this particular form is applicable or not.
Y
is a a string of unknown length and content, surrounded by parenthesis.
Also: Y
is optional; it doesn't always appear in a string with Z
and X
. So, I want to be able to extract the numbers from all of these strings:
10 Z
20 (foo) Z
30 (bar) Z
Right now, I have a regex that will capture the first one:
([0-9]+) +Z
My problem is that I don't know how to construct a regex that will match a series of characters if and only if they're enclosed in parenthesis. Can this be done in a single regex?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…