regex

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

Hi,
I'm beginning to understand regex (regular expressions), but I think there's
a better way to do the next thing. I use 2 steps, is it possible to do it in
1 step?

<img somestuff src="found.gif" otherstuff>

Is it possible to get the index and length of 'found.gif' in 1 regex match?

Thanks in advance
Frank
 
Frank,
yes.

See Captures and Groups collections in Match. Both Capture and Group objects
have Index and Length properties.

HTH
Alex
 
Frank,

here is sample code

Regex r = new Regex(@"<img.+src=(+.+).+");

string str = "<img somestuff src=\"found.gif\" otherstuff>";

Match m = r.Match(str);

if (m.Success) {

foreach (Group g in m.Groups) {

Console.WriteLine("Group: {0}, starts {1}, length {2}",

g.Value, g.Index, g.Length);

}

}

This produces output:

Group: <img somestuff src="found.gif" otherstuff>, starts 0, length 43
Group: found.gif, starts 21, length 9

You can produce same with Captures



Alex
 
Noticed that some backslahes were eaten by paste. Check that reg.exp is
Regex(@"\<img.+src\=\x22(\w+\.\w+)\x22.+");

Alex
 

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