The following code snippet will guide you on how to search a string with Regular Expressions with Grouping mechanism and return the value by the group name?
public string RegExGetValueByGroupName(string input, string regExPattern, string groupName)
{
IEnumerable<Group> matchedList = new Regex(regExPattern)
.Match(input).Groups.Where(grp => string.Equals(grp.Name, groupName));
return matchedList.Any() ? matchedList.FirstOrDefault().Value : null;
}
No comments:
Post a Comment