Skip to content

RegEx _ IsMatchFormatter

axunonb edited this page Mar 30, 2022 · 3 revisions

The IsMatchFormatter lets you evaluate a RegEx regular expression to control the output.

Syntax Details

{ Any Value : ismatch(RegExpression) : output if match | output if no match ** }

*RegExpression: Special characters like (){}:\ must be escaped with \ for SmartFormat.

Configuration:

string Name: default is ismatch
The name to use a named formatter

char SplitChar: default is '|'

RegexOptions RegexOptions: default is RegexOption.None

string PlaceholderNameForMatches: default is "m".
The name of the placeholder used to output matching RegularExpression.Group values.

Examples

Test for match with constant output

var data = new KeyValuePair<string,object?>("theKey", "Some123Content");
Smart.Format("{theKey:ismatch(^.+123.+$):Content if match|Content for no match}", data);
// outputs: "Content if match"

Output contains input variable

var data = new KeyValuePair<string,object?>("theKey", "Some123Content");
Smart.Format("{theKey:ismatch(^.+123.+$):Okay - {}|Content for no match}", data);
// outputs: "Okay - Some123Content"

IsMatch as a nested placeholder to List

var myList = new List<int> {100, 200, 300};

Smart.Format("{0:list:{:ismatch(^100|200|999$):{:0.00}|'no match'}|, | and }", myList));
// outputs: "100.00, 200.00 and 'no match'"

Smart.Format("{0:list:{:ismatch(^100|200|999$):'match'|'no match'}|, | and }", myList));
// outputs: 'match', 'match' and 'no match'

Complex RegEx to test for valid email address

Say we want to include in the output, whether an email is valid (simplified):

var emailRegEx = "^((\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)\s*[;]{0,1}\s*)+$";

// ask a little helper to escape the RegEx
var escaped = EscapedLiteral.EscapeCharLiterals('\\', emailRegEx, 0, emailRegEx.Length, true);

// insert the escaped literal from the helper
Smart.Format("Email {0:ismatch("^\(\(\\w+\([-+.]\\w+\)*@\\w+\([-.]\\w+\)*\\.\\w+\([-.]\\w+\)*\)\\s*[;]\{0,1\}\\s*\)+$"):{} is valid|{} NOT valid}", "[email protected]");
// outputs: "Email [email protected] is valid"

Output matching RegularExpression.Group values

We'll evalute this argument with IsMatchFormatter:

KeyValuePair<string, object> arg = new("theValue", "Some123Content");

The placeholder 'm' used below is for the collection of matching RegEx group values generated by IsMatchFormatter. The name of this placeholder can be set with IsMatchFormatter.PlaceholderNameForMatches.

The collection has at least one entry for a successful match. See more details in the Microsoft docs for the GroupCollection class.

a) List all matching group values
Smart.Format("{theValue:ismatch(^.+\\(1\\)\\(2\\)\\(3\\).+$):Matches for '{}'\\: {m:list:| - }|No match}", arg);
// Outputs: "Matches for 'Some123Content': Some123Content - 1 - 2 - 3"
b) Show specific matching group values with their index in the list
// 
Smart.Format("{theValue:ismatch(^.+\\(1\\)\\(2\\)\\(3\\).+$):First 2 matches in '{}'\\: {m[1]} and {m[2]}|No match}", arg);
// Outputs: "First 2 matches in 'Some123Content': 1 and 2"
Clone this wiki locally