Regular expressions

Matching in a query string parameter

I feel documentation on any regex is important and they are super hard to read / understand.

1
2
3
4
5
6
7
8
9
10
11
/// <summary>
/// $ -> String interpolation
/// @" -> Starts a verbatim string literal.
/// ( -> Logical grouping of part of an expression.
/// (\&|\?) -> Start with `&` or `?`
/// ? -> Result of `(\&|\?)` must be 0 or 1
/// \b -> Matches a word boundary
/// {SomeAttribute.Hoe}=1 -> String interpolation, if `SomeAttribute.Hoe` was `42` then this would add `42=1`
/// \b -> Matches a word boundary
/// </summary>
private readonly string MyQueryStringParamRegex = $@"((\&|\?)?)\b{SomeAttribute.Hoe}=1\b";

You could then use this in code as

1
2
3
4
if (new Regex(MyQueryStringParamRegex).IsMatch(query))
{
// do something based on the match
}

Matching email address

Sweet example from computerhope.com to match an Email address.

Email Matching Example

References