|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Regular Expression Question IIHi,
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 retValue=roles[0].Groups[0].ToString();
Show quote "Axel Dahmen" wrote: > 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 ------------- Show quote "jhgonzales" <jhgonza***@discussions.microsoft.com> wrote in message news:3E95135B-0262-4978-84E9-4FD81047D496@microsoft.com... > retValue=roles[0].Groups[0].ToString(); > > "Axel Dahmen" wrote: > > > 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]. -- Show quoteHTH, Kevin Spencer Microsoft MVP ..Net Developer To a tea you esteem a hurting back as a wallet. "Axel Dahmen" <NO_SPAM@NoOneKnows.invalid> wrote in message news:OCiIw67MGHA.1676@TK2MSFTNGP09.phx.gbl... > 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" <jhgonza***@discussions.microsoft.com> wrote in message > news:3E95135B-0262-4978-84E9-4FD81047D496@microsoft.com... >> retValue=roles[0].Groups[0].ToString(); >> >> "Axel Dahmen" wrote: >> >> > 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 -------------------- Show quote "Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message news:OVnNyD9MGHA.2276@TK2MSFTNGP15.phx.gbl... > 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" <NO_SPAM@NoOneKnows.invalid> wrote in message > news:OCiIw67MGHA.1676@TK2MSFTNGP09.phx.gbl... > > 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" <jhgonza***@discussions.microsoft.com> wrote in message > > news:3E95135B-0262-4978-84E9-4FD81047D496@microsoft.com... > >> retValue=roles[0].Groups[0].ToString(); > >> > >> "Axel Dahmen" wrote: > >> > >> > 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 ----------- Show quote "Axel Dahmen" <NO_SPAM@NoOneKnows.invalid> wrote in message news:%23vQ0MB%23MGHA.456@TK2MSFTNGP15.phx.gbl... > 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" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message > news:OVnNyD9MGHA.2276@TK2MSFTNGP15.phx.gbl... > > 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" <NO_SPAM@NoOneKnows.invalid> wrote in message > > news:OCiIw67MGHA.1676@TK2MSFTNGP09.phx.gbl... > > > 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" <jhgonza***@discussions.microsoft.com> wrote in message > > > news:3E95135B-0262-4978-84E9-4FD81047D496@microsoft.com... > > >> retValue=roles[0].Groups[0].ToString(); > > >> > > >> "Axel Dahmen" wrote: > > >> > > >> > 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. -- Show quoteHTH, Kevin Spencer Microsoft MVP ..Net Developer To a tea you esteem a hurting back as a wallet. "Axel Dahmen" <NO_SPAM@NoOneKnows.invalid> wrote in message news:usPuBg%23MGHA.3556@TK2MSFTNGP10.phx.gbl... > Sorry, y'all, > > found my mistake: The "+" should have been WITHIN the capture. > > Thanks to all trying to help!!! > Axel > > > > ----------- > "Axel Dahmen" <NO_SPAM@NoOneKnows.invalid> wrote in message > news:%23vQ0MB%23MGHA.456@TK2MSFTNGP15.phx.gbl... >> 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" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message >> news:OVnNyD9MGHA.2276@TK2MSFTNGP15.phx.gbl... >> > 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" <NO_SPAM@NoOneKnows.invalid> wrote in message >> > news:OCiIw67MGHA.1676@TK2MSFTNGP09.phx.gbl... >> > > 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" <jhgonza***@discussions.microsoft.com> wrote in message >> > > news:3E95135B-0262-4978-84E9-4FD81047D496@microsoft.com... >> > >> retValue=roles[0].Groups[0].ToString(); >> > >> >> > >> "Axel Dahmen" wrote: >> > >> >> > >> > 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 >> > >> > >> > >> > >> > >> > >> > > >> > > >> > >> > >> >> > > |
|||||||||||||||||||||||