Help with Regex in CSharp

  • Thread starter Thread starter Holger Kasten
  • Start date Start date
H

Holger Kasten

Hi there, could some one give me some help on how to use regex
to do the following:

I have a string: "x+y+foo(z)*abc(fun(a+bbb)*y)"

And woould like to get two lists containing that:

list 1: x,y,z,a,bbb,y
list 2: foo,abc,fun

Thx for you help,

Holger
 
Holger said:
Hi there, could some one give me some help on how to use regex
to do the following:

I have a string: "x+y+foo(z)*abc(fun(a+bbb)*y)"

And woould like to get two lists containing that:

list 1: x,y,z,a,bbb,y
list 2: foo,abc,fun

Thx for you help,

Holger

for the first:
\b[a-zA-Z]+(?![(])\b

for the second:
\b[a-zA-Z]+(?=[(])\b


Hans Kesting
 
I tried the following:

Regex reg1 = new Regex("\b[a-zA-Z]+(?![(])\b");
Regex reg2 = new Regex("\b[a-zA-Z]+(?=[(])\b");
MatchCollection mc1 = reg1.Matches("x+y+foo(z)*abc(fun(a+bbb)*y)");
MatchCollection mc2 = reg2.Matches("x+y+foo(z)*abc(fun(a+bbb)*y)");

But it did not work, the collections were empty. What am i doing wrong?
 
Holger said:
I tried the following:

Regex reg1 = new Regex("\b[a-zA-Z]+(?![(])\b");
Regex reg2 = new Regex("\b[a-zA-Z]+(?=[(])\b");
MatchCollection mc1 = reg1.Matches("x+y+foo(z)*abc(fun(a+bbb)*y)");
MatchCollection mc2 = reg2.Matches("x+y+foo(z)*abc(fun(a+bbb)*y)");

But it did not work, the collections were empty. What am i doing
wrong?

It tried those expressions in "The Regulator" and there they worked.
Now I tried it in SnippetCompiler, and indeed they failed.
Some investigating showed that the "\b" was the problem. Apparently
this is not recognised as MSDN says it should be.

New try:
Regex reg1 = new Regex("[a-zA-Z]+(?=[-+*/)])");
Regex reg2 = new Regex("[a-zA-Z]+(?=[(])");

Hans Kesting
 
Holger said:
I tried the following:

Regex reg1 = new Regex("\b[a-zA-Z]+(?![(])\b");
Regex reg2 = new Regex("\b[a-zA-Z]+(?=[(])\b");
MatchCollection mc1 = reg1.Matches("x+y+foo(z)*abc(fun(a+bbb)*y)");
MatchCollection mc2 = reg2.Matches("x+y+foo(z)*abc(fun(a+bbb)*y)");

But it did not work, the collections were empty. What am i doing wrong?

Mind you, I haven't tried the expressions themselves. But you should use
verbatim string literals to pass the expressions, otherwise you'll have
problems because of the backslashes. So, try to use
@"\b[a-zA-Z]+(?![(])\b" (note the @ in front), maybe it'll work better.


Oliver Sturm
 
Oliver said:
Holger said:
I tried the following:

Regex reg1 = new Regex("\b[a-zA-Z]+(?![(])\b");
Regex reg2 = new Regex("\b[a-zA-Z]+(?=[(])\b");
MatchCollection mc1 = reg1.Matches("x+y+foo(z)*abc(fun(a+bbb)*y)");
MatchCollection mc2 = reg2.Matches("x+y+foo(z)*abc(fun(a+bbb)*y)");

But it did not work, the collections were empty. What am i doing
wrong?

Mind you, I haven't tried the expressions themselves. But you should
use verbatim string literals to pass the expressions, otherwise
you'll have problems because of the backslashes. So, try to use
@"\b[a-zA-Z]+(?![(])\b" (note the @ in front), maybe it'll work
better.

Oliver Sturm

Of course, that was the problem. I should have seen it :-(
At least it now works! (tested it)

Hans Kesting
 
Holger said:
Hi there, could some one give me some help on how to use regex
to do the following:

I have a string: "x+y+foo(z)*abc(fun(a+bbb)*y)"

And woould like to get two lists containing that:

list 1: x,y,z,a,bbb,y
list 2: foo,abc,fun

Thx for you help,

Holger

This does not directly answer your question but for anyone out there who
uses regular expressions a lot, this tool -->
http://www.ultrapico.com/Expresso.htm - has been a great help to me.
 
Thx a lot, that works great.

Is there also an easy way to replace:

So that:

x+y+foo(z)*abc(fun(a+bbb)*y)

would turn to:

}x{+}y{+<foo>(}z{)*<abc>(<fun>(}a{+}bbb{)*}y{)
 
I also realized now that by using "a*b+c"
as input the varibale c is not found.

Any ideas?
 
: Hi there, could some one give me some help on how to use regex
: to do the following:
:
: I have a string: "x+y+foo(z)*abc(fun(a+bbb)*y)"
:
: And woould like to get two lists containing that:
:
: list 1: x,y,z,a,bbb,y
: list 2: foo,abc,fun
:
: Thx for you help,

Another possibility is to write a parser so you can examine an AST.
For example code -- in C#, despite what the URL might lead you to
believe -- that parses the expression above, see

http://gbacon.blogspot.com/2005/09/simple-expression-parser-in-c.html

Greg
 
Try :
Match
match=Regex.Match(@"x+y+foo(z)*abc(fun(a+bbb)*y)",@"^(?:(?:(?:(?<func>[a-zA-
Z]+)\()|(?<var>[a-zA-Z]+))[+-/*()]*)*$");
foreach(Capture c in match.Groups["func"].Captures)
MessageBox.Show("func:"+c);
foreach(Capture c in match.Groups["var"].Captures)
MessageBox.Show("var:"+c);

Hope it helps

Ludovic Soeur.
 

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

Back
Top