|
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 | |
Share:
|
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.
--
Göran Andersson
_____ http://www.guffa.com | | |
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
BTW using your expression generated the following
(a + «EXPR»)
Why does it not pick up the initial opening bracket?
S | | |
On Jul 18, 9:52*am, Simon Woods <simonjwo...@hotmail.comwrote:
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
Since you mention you were new to Regex, I highly suggest you download
Expresso: http://www.ultrapico.com/Expresso.htm
Expresso is a great tool to use for testing out regular expressions,
and has came to my rescue a few times. It seems their site is down
right now, but hopefully it'll be back up soon.
Thanks,
Seth Rowe [MVP] http://sethrowe.blogspot.com/ | | |
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
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 | | |
rowe_newsgroups wrote:
On Jul 18, 9:52 am, Simon Woods <simonjwo...@hotmail.comwrote:
>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
Since you mention you were new to Regex, I highly suggest you download
Expresso: http://www.ultrapico.com/Expresso.htm
Expresso is a great tool to use for testing out regular expressions,
and has came to my rescue a few times. It seems their site is down
right now, but hopefully it'll be back up soon.
Thanks,
Seth Rowe [MVP] http://sethrowe.blogspot.com/
Thx S | | |
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 | | |
On Jul 19, 4:04*pm, Göran Andersson <gu...@guffa.comwrote:
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 -
^\(.*\)$ won't work it should be ^\([^\(]*)$ | | |
keremskusmezer wrote:
On Jul 19, 4:04 pm, Göran Andersson <gu...@guffa.comwrote:
>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 -
^\(.*\)$ won't work it should be ^\([^\(]*)$
No, that doesn't work.
You probably didn't understand what the pattern was intended for. It was
an example of a pattern matching a string starting with '(' and ending
with ')', for example the strings "(a)" or "(a+(b+c))". Your pattern
would match the first string, but not the second.
It was an example to demonstrate the difference between matching the
parentheses from the inside or from the outside. Se what I wrote earlier
in the thread for a pattern to match the innermost parentheses.
--
Göran Andersson
_____ http://www.guffa.com | | |
On Jul 23, 12:30*am, Göran Andersson <gu...@guffa.comwrote:
keremskusmezer wrote:
On Jul 19, 4:04 pm, Göran Andersson <gu...@guffa.comwrote:
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 -
^\(.*\)$ won't work it should be ^\([^\(]*)$
No, that doesn't work.
You probably didn't understand what the pattern was intended for. It was
an example of a pattern matching a string starting with '(' and ending
with ')', for example the strings "(a)" or "(a+(b+c))". Your pattern
would match the first string, but not the second.
It was an example to demonstrate the difference between matching the
parentheses from the inside or from the outside. Se what I wrote earlier
in the thread for a pattern to match the innermost parentheses.
--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -
- Show quoted text -
I see sry, you planned to parse the parenthesis in a recursive manner
going from outside to inside. | | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
6 posts
views
Thread by Tony C |
last post: by
|
8 posts
views
Thread by Bibe |
last post: by
|
4 posts
views
Thread by H |
last post: by
|
6 posts
views
Thread by Dave |
last post: by
|
4 posts
views
Thread by henrik |
last post: by
|
9 posts
views
Thread by jmchadha |
last post: by
|
2 posts
views
Thread by Alex Maghen |
last post: by
|
3 posts
views
Thread by =?Utf-8?B?TmF2ZWVu?= |
last post: by
|
10 posts
views
Thread by supercrossking |
last post: by
|
reply
views
Thread by Support Desk |
last post: by
| | | | | | | | | | |