Thursday 12 December 2019

C#.NET - Regular Expressions - Search Text by Group Name

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: