Help re RegEx

S

Simon Woods

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
 
G

Göran Andersson

Simon said:
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.
 
S

Simon Woods

Göran Andersson said:
Simon said:
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
 
S

Simon Woods

Simon said:
Göran Andersson said:
Simon said:
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
 
R

rowe_newsgroups

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/
 
G

Göran Andersson

Simon said:
Simon said:
Göran Andersson said:
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 ):

^\(.*\)$
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.
 
K

keremskusmezer

Simon said:
Simon said:
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 ):

^\(.*\)$




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

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 ^\([^\(]*)$
 
G

Göran Andersson

keremskusmezer said:
Simon said:
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.
 
K

keremskusmezer

keremskusmezer said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

string replacement - regex? 2
Need help with regex expression 6
Regex help 10
Insert character using Regex 4
Regex help! 3
Newbie question about Regex 8
Help re Delegates and Lambdas 1
RegEx Format Help 4

Top