-
-
Notifications
You must be signed in to change notification settings - Fork 105
v2 IsMatch
axunonb edited this page Feb 6, 2022
·
1 revision
The IsMatchFormatter
lets you evaluate regular expressions to control the output.
var data = new Dictionary<string,object>() { {"theKey", "Some123Content"}};
Smart.Format("{theKey:ismatch(^.+123.+$):Okay - {}|No match content}", data);
// result: Okay - Some123Content
Smart.Format("{theKey:ismatch(^.+123.+$):Fixed content if match|No match content}", data);
// result: Fixed content if match
Working with lists:
var myList = new List<int> {100, 200, 300};
Smart.Format(CultureInfo.InvariantCulture, "{0:list:{:ismatch(^100|200|999$):{:0.00}|'no match'}|, | and }", myList));
// result: 100.00, 200.00 and 'no match'
Smart.Format(CultureInfo.InvariantCulture, "{0:list:{:ismatch(^100|200|999$):'match'|'no match'}|, | and }", myList));
// result: 'match', 'match' and 'no match'
{ Any Value : ismatch(RegExpression) : output if match | output if no match ** }
Note:
The IsMatchFormatter is not included in the default SmartFormatter instance. In case you'll need it, it must be added like so:
Smart.Default.FormatterExtensions.Add(new IsMatchFormatter { RegexOptions = RegexOptions.CultureInvariant });
In the options part of the IsMatchFormatter not all RegEx characters are valid. E.g.: No curly braces or parenthesis. This is because the same parser is used as for all other extensions.
- 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