-
-
Notifications
You must be signed in to change notification settings - Fork 107
RegEx _ IsMatchFormatter
The IsMatchFormatter
lets you evaluate a RegEx
regular expression to control the output.
{ Any Value : ismatch(RegExpression) : output if match | output if no match ** }
*RegExpression: Special characters like (){}:\
must be escaped with \
for SmartFormat.
string Name
: default isismatch
The name to use a named formatter
char SplitChar
: default is'|'
RegexOptions RegexOptions
: default isRegexOption.None
string PlaceholderNameForMatches
: default is "m".
The name of the placeholder used to output matchingRegularExpression.Group
values.
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"
var data = new KeyValuePair<string,object?>("theKey", "Some123Content");
Smart.Format("{theKey:ismatch(^.+123.+$):Okay - {}|Content for no match}", data);
// outputs: "Okay - Some123Content"
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'
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}", "joe@specimen.com");
// outputs: "Email joe@specimen.com is valid"
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.
Smart.Format("{theValue:ismatch(^.+\\(1\\)\\(2\\)\\(3\\).+$):Matches for '{}'\\: {m:list:| - }|No match}", arg);
// Outputs: "Matches for 'Some123Content': Some123Content - 1 - 2 - 3"
//
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"
- Syntax, Terminology
- Placeholders and Nesting
- string.Format Compatibility
- Character Literals in Format Strings
- HTML With CSS or JavaScript
- Data Source Extensions
- Default _ DefaultFormatter
- Lists _ ListFormatter
- Choose _ ChooseFormatter
- Condition _ ConditionalFormatter
- Null _ NullFormatter
- SubString _ SubStringFormatter
- RegEx _ IsMatchFormatter
- Pluralization _ PluralLocalizationFormatter
- Localization _ LocalizationFormatter
- Templates _ TemplateFormatter
- TimeSpan _ TimeFormatter
- XML _ XElementFormatter
- Extension Methods
- Home
- Common Pitfalls
- HTML with CSS or JavaScript
- Overview
- Main Features
- Formatters
- Extra Features
- Console and StringBuilder
- TemplateFormatter
- SmartSettings to control Smart.Format behavior
- Additional Info
- License