Articles in categories

Now on Amazon

Usefull regex patterns

A list with some usefull regex patterns I needed to use. You can find a lot of ready to use regex patterns on RegExLib. Some patterns I got with a little help from StackOverflow.

Find all connected words before leading and after trailing space.

Text: "This is anexampleof what I want as a result".
Result: "anexampleof" when searching for "example"

Regex: 

"\S*example\S*"

C# usage:

MatchCollection m1 = Regex.Matches(content, @"\S*" + searchword + @"\S*", RegexOptions.IgnoreCase | RegexOptions.Multiline);

 

Extract all <a> tags and href attribute from html block

Finds all the <a> tags from an html content block.

Regex:

"(<a.*?>.*?</a>)"

C# usage:

MatchCollection m1 = Regex.Matches(content, @"(<a.*?>.*?</a>)", RegexOptions.Singleline);

 After you've got the <a> tag collection you can get the href attribute value

Regex:

"href=\""(.*?)\"""

C# usage:

Match m2 = Regex.Match(value, @"href=\""(.*?)\""", RegexOptions.Singleline);

Full C# code to retrieve <a> tags from an html content block and get their href attribute value:

MatchCollection m1 = Regex.Matches(content, @"(<a.*?>.*?</a>)", RegexOptions.Singleline);
foreach (Match m in m1)
{
	string value = m.Groups[1].Value;
	string hrefLink = string.Empty;

	// Get href attribute.
	Match m2 = Regex.Match(value, @"href=\""(.*?)\""", RegexOptions.Singleline);
	if (m2.Success)
	{
		hrefLink = m2.Groups[1].Value;

		// do something with the retrieve value
	}
}