question about regex
i'v got code
s = Regex.Match(item.Value, @"\/>.*?\*>", RegexOptio开发者_高级运维ns.IgnoreCase).Value;
it returns string like '/>test*>', i can replace symbols '/>' and '*>', but how can i return string without this symbols , only string 'test' between them?
You can save parts of the regex by putting ()
's around the area. so for your example:
// item.Value == "/>test*>"
Match m = Regex.Match(item.Value, @"\/>(.*?)\*>");
Console.WriteLine(m.Groups[0].Value); // prints the entire match, "/>test*>"
Console.WriteLine(m.Groups[1].Value); // prints the first saved group, "test*"
I also removed RegexOptions.IgnoreCase
because we aren't dealing with any letters specifically, whats an uppercase />
look like? :)
You can group patterns inside the regx and get those from the match
var match= Regex.Match(item.Value, @"\/>(?<groupName>.*)?\*>", RegexOptions.IgnoreCase);
var data= match.Groups["groupName"].Value
You can also use look-ahead and look-behind. For your example it would be:
var value = Regex.Match(@"(?<=\/>).*?(?=\*>)").Value;
精彩评论