Simon Woods wrote:
Simon Woods wrote:
Göran Andersson wrote:
Simon Woods wrote:
Hi
I'm new to Regular Expressions so ...
I trying to work out regular expressions to parse the following
(a + (b + c))
I really want to replace it with
Replacement 1: (a + <Exp0>)
Replacement 2: <Exp1>
I'm okay using the MatchEvaluator delegate so I can keep track of
what expr, but I'm struggling to work out how to express the
pattern. So far I've got "\(*\)*".
I thought that \( would indicate an opening bracket followed by
anything followed by a closing bracket. But I'm obviously wrong..
Thx IA
Simon
The pattern that you have written matches any number of opening
brackets (even zero), followed by any number of closing brackets. So,
it would exactly match any of (but not limited to) these strings:
"()"
"(((((((((()"
"()))))))"
"("
")"
""
"((())))))))"
You probably want this:
"\([^\(\)]+\)"
\( matches the opening bracket
[^\(\)]+ matches any character except brackets, one or more times
\) matches the closing bracket
As the contents between the brackets can't contain brackets, the
pattern will match the innermost expression.
okay thx very much ... Göran.
Does this assume an opening bracket at the beginning and a closing
bracket at the end - or does it just assume any chars before and any
chars after - or do I need to put "*\([^\(\)]+\)*" or something
A regular expression matches a part of the string. If you want it to
match from the beginning of the string you have to put ^ at the start of
the pattern, and if you want it to match to the end of the string you
have to put $ at the end of the pattern.
Example matching a string that starts with ( and ends with ):
^\(.*\)$
BTW using your expression generated the following
(a + «EXPR»)
Why does it not pick up the initial opening bracket?
S
Actually, it looks good once I put it into a loop ... thanks very much
... I'm not sure why it matches the internal brackets before the
external ... could you explain.
Thx though
S
As the pattern matches the starting and ending brackets, and anything
between them that is not a bracket, you will get the innermost
expression. If you would match just the first starting bracket with the
first ending bracket, you would get the starting bracket of the outer
expression and the ending bracket of the inner expression.
It's much easier to handle the brackets from inside out. You can do it
from the outside in, but then you would probably not use a regular
expression at all, but rather parsing the string character by character.
--
Göran Andersson
_____
http://www.guffa.com-Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -