How to match an enclosing substring using regex in .NET
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 th开发者_开发知识库e middle "3" or "anything" How do I do this in .NET regex or otherwise?
(?<=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
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!
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)
"\\*"
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"]);
}
}
}
精彩评论