Groups | Blog | Home
all groups > dotnet framework > february 2006 >

dotnet framework : Regular Expression Question II


jhgonzales
2/16/2006 3:05:28 PM
retValue=roles[0].Groups[0].ToString();

[quoted text, click to view]
Axel Dahmen
2/16/2006 8:26:39 PM
Hi,

If I write

roles=Regex.Matches(myRoles,@"prefix([^,])+,");

then

retValue=roles[0].Groups[0].Captures[0].ToString();

returns e.g. "prefix_some_value,".

How can I retrieve just what's within the parentheses, e.g. "_some_value"?

TIA,
Axel Dahmen

Kevin Spencer
2/17/2006 9:46:31 AM
Hi Axel,

First, Group 0 in .Net is always the entire match. Second, you would need to
create a Group to do this. Example:

,@"(prefix)([^,])+,"

Then you would use Groups[1].

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
To a tea you esteem
a hurting back as a wallet.


[quoted text, click to view]

Axel Dahmen
2/17/2006 1:35:48 PM
Nope, doesn't work.. I still get the whole pattern string instead of only
what's within the parentheses..

Any other suggestions, please? (I'm using .NET 1.1)

TIA,
Axel Dahmen



-------------
[quoted text, click to view]

Kevin Spencer
2/17/2006 2:02:28 PM
Yes, and you don't need the second group! It's a character class. No need to
group it unless you want a group from it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
To a tea you esteem
a hurting back as a wallet.


[quoted text, click to view]

Axel Dahmen
2/17/2006 5:36:24 PM
Thanks for helping, Kevin,

but from your example, doesn't that actually yield *two* groups?

(prefix) - and - ([^,])+
~~~~~~~~ ~~~~~~~


This is what I get if I use roles[0].Groups[0].ToString():

abcde

And this I get if I use roles[0].Groups[1].ToString():

e

It always only yields the last character in the capture.

(given my original pattern: Regex.Matches("prefixabcde," ,
@"prefix([^,])+," );)


I can't possibly find my error here....

Quite baffled,
Axel



--------------------
[quoted text, click to view]

Axel Dahmen
2/17/2006 6:31:34 PM
Sorry, y'all,

found my mistake: The "+" should have been WITHIN the capture.

Thanks to all trying to help!!!
Axel



-----------
[quoted text, click to view]

AddThis Social Bookmark Button