0

I'm trying to match * in id=resultsStats>*<nobr> to extract the middle bit. This would match e.g. id=resultsStats>3<nobr> id=resultsStats>anything<nobr>

so I can extract the middle "3" or "anything" How do I do this in .NET regex or otherwise?

user4
  • 734
  • 1
  • 5
  • 16
  • 7
    Obligatory link: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Andrew Savinykh Jun 10 '11 at 22:22

4 Answers4

3
(?<=id=resultsStats>).+?(?=<nobr>)

Use * instead of + if content is optional rather than required.

Example of use (F#):

open System.Text.RegularExpressions

let tryFindResultsStats input =
    let m = Regex.Match (input,
                         "(?<=id=resultsStats>).+?(?=<nobr>)",
                         RegexOptions.Singleline)
    if m.Success then Some m.Value else None
ildjarn
  • 62,044
  • 9
  • 127
  • 211
0

I'm not a regex expert but something like this might work:

@"\>\*{1}\<"

This means "match a single asterisk between the lt/gt characters". You just need to make sure you escape the asterisk because it has special meaning in regular expressions.

Hope this helps!

sakuntala
  • 37
  • 3
0

If you are looking to capture a * then you need to escape it with a backslash. Note that if you are doing this within a string it is safest to escape the backslash as well (Although technically \* isn't valid and will work)

"\\*"
Guvante
  • 18,775
  • 1
  • 33
  • 64
0

Try this:

using System;
using System.Text.RegularExpressions;

namespace SO6312611
{
    class Program
    {
        static void Main()
        {
            string input = "id=resultsStats>anything<nobr>";
            Regex r = new Regex("id=resultsStats>(?<data>[^<]*)<nobr>");
            Match m = r.Match(input);
            Console.WriteLine("Matched: >{0}<", m.Groups["data"]);
        }
    }
}
Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
  • Why include data you don't care about in the match? Also, he said anything until ``, not anything until another arbitrary tag. – ildjarn Jun 10 '11 at 22:50