Regular Expression Question II

  • Thread starter Thread starter Axel Dahmen
  • Start date Start date
A

Axel Dahmen

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



-------------
jhgonzales said:
retValue=roles[0].Groups[0].ToString();

Axel Dahmen said:
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
 
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.


Axel Dahmen said:
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



-------------
jhgonzales said:
retValue=roles[0].Groups[0].ToString();

Axel Dahmen said:
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
 
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



--------------------
Kevin Spencer said:
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.


Axel Dahmen said:
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



-------------
jhgonzales said:
retValue=roles[0].Groups[0].ToString();

:

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
 
Sorry, y'all,

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

Thanks to all trying to help!!!
Axel



-----------
Axel Dahmen said:
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



--------------------
Kevin Spencer said:
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.


Axel Dahmen said:
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



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

:

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
 
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.


Axel Dahmen said:
Sorry, y'all,

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

Thanks to all trying to help!!!
Axel



-----------
Axel Dahmen said:
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



--------------------
Kevin Spencer said:
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.


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



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

:

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
 
Back
Top