|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Regex - words extractionHello!
I have the following string: <wordX::wordY[wordZ]> I would like to extract from that string three words wordX, wordY and wordZ by Regex usage. Regards Saso "Saso" <S***@discussions.microsoft.com> wrote in message more or less:news:4D993A15-C126-45D5-8130-53DC9B4CCFBC@microsoft.com... > Hello! > > I have the following string: <wordX::wordY[wordZ]> > > I would like to extract from that string three words > wordX, wordY and wordZ by Regex usage. r = new Regex(@"\<[^:]+::[^[]+\[[^\]]+\]\>"); matches = r.Match(str); matches.Group(1) matches.Group(2) matches.Group(3) Show quote > > Regards > Saso Can you put exact code in written in c#. I tryed as follows, but nothing
happens. Regex r = new Regex(@"\<[^:]+::[^[]+\[[^\]]+\]\>"); MatchCollection matches = r.Matches("<wordx::wordy[wordz]>"); foreach (Match s in matches) { Console.WriteLine(s.Value); } Show quote "Ben Voigt" wrote: > > "Saso" <S***@discussions.microsoft.com> wrote in message > news:4D993A15-C126-45D5-8130-53DC9B4CCFBC@microsoft.com... > > Hello! > > > > I have the following string: <wordX::wordY[wordZ]> > > > > I would like to extract from that string three words > > wordX, wordY and wordZ by Regex usage. > > more or less: > > r = new Regex(@"\<[^:]+::[^[]+\[[^\]]+\]\>"); > matches = r.Match(str); > matches.Group(1) > matches.Group(2) > matches.Group(3) > > > > > Regards > > Saso > > > "Saso" <S***@discussions.microsoft.com> wrote in message Forgot the captures. Here's the code:news:9F6258F7-1C31-43AA-BC96-6DE828CED1D0@microsoft.com... > Can you put exact code in written in c#. I tryed as follows, but nothing > happens. > > Regex r = new Regex(@"\<[^:]+::[^[]+\[[^\]]+\]\>"); > > MatchCollection matches = r.Matches("<wordx::wordy[wordz]>"); > foreach (Match s in matches) > { > Console.WriteLine(s.Value); > } Regex r = new Regex(@"^\<([^:]+)::([^[]+)\[([^\]]+)\]\>$"); Match m = r.Match("<wordx::wordy[wordz]>"); System.Diagnostics.Debug.WriteLine(m.Success); foreach (Group s in m.Groups) { System.Diagnostics.Debug.WriteLine(s.Value); } Show quote > > > "Ben Voigt" wrote: > >> >> "Saso" <S***@discussions.microsoft.com> wrote in message >> news:4D993A15-C126-45D5-8130-53DC9B4CCFBC@microsoft.com... >> > Hello! >> > >> > I have the following string: <wordX::wordY[wordZ]> >> > >> > I would like to extract from that string three words >> > wordX, wordY and wordZ by Regex usage. >> >> more or less: >> >> r = new Regex(@"\<[^:]+::[^[]+\[[^\]]+\]\>"); >> matches = r.Match(str); >> matches.Group(1) >> matches.Group(2) >> matches.Group(3) >> >> > >> > Regards >> > Saso >> >> >> Saso,
Here's your RegEx: \<(.*)\:\:(.*)\[(.*)\]\> Download a product called The Regulator. It'll come up in Google. Stephan Saso wrote: Show quote > Hello! > > I have the following string: <wordX::wordY[wordZ]> > > I would like to extract from that string three words > wordX, wordY and wordZ by Regex usage. > > Regards > Saso Ssamuel, I tryed your regex, but it seems that nothing happens,
MatchCollection is empy, Groups count is one. Any suggestion? Regards Show quote "ssamuel" wrote: > Saso, > > Here's your RegEx: > > \<(.*)\:\:(.*)\[(.*)\]\> > > Download a product called The Regulator. It'll come up in Google. > > > Stephan > > > > Saso wrote: > > Hello! > > > > I have the following string: <wordX::wordY[wordZ]> > > > > I would like to extract from that string three words > > wordX, wordY and wordZ by Regex usage. > > > > Regards > > Saso > > Saso,
The Match interface is obfuscated. Try: Regex re = new Regex(@"\<(.*)\:\:(.*)\[(.*)\]\>"); string[] list = re.Split("<wordX::wordY[wordZ]>"); The first match is nothing, but 2, 3, and 4 (index 1, 2, and 3) are your results. Stephan Saso wrote: Show quote > Ssamuel, I tryed your regex, but it seems that nothing happens, > MatchCollection is empy, Groups count is one. > > Any suggestion? > > Regards > > "ssamuel" wrote: > > > Saso, > > > > Here's your RegEx: > > > > \<(.*)\:\:(.*)\[(.*)\]\> > > > > Download a product called The Regulator. It'll come up in Google. > > > > > > Stephan > > > > > > > > Saso wrote: > > > Hello! > > > > > > I have the following string: <wordX::wordY[wordZ]> > > > > > > I would like to extract from that string three words > > > wordX, wordY and wordZ by Regex usage. > > > > > > Regards > > > Saso > > > > Ssamule, I just solved the problem, I mixed up the parameters of Regex.Match
method :). It happens. Thank you. Saso Show quote "ssamuel" wrote: > Saso, > > Here's your RegEx: > > \<(.*)\:\:(.*)\[(.*)\]\> > > Download a product called The Regulator. It'll come up in Google. > > > Stephan > > > > Saso wrote: > > Hello! > > > > I have the following string: <wordX::wordY[wordZ]> > > > > I would like to extract from that string three words > > wordX, wordY and wordZ by Regex usage. > > > > Regards > > Saso > > |
|||||||||||||||||||||||