Skip to content

Extend IsMatchFormatter, add docs to rc.2#245

Merged
axunonb merged 1 commit intoaxuno:version/v3.0from
axunonb:pr-extended-ismatchformatter
Feb 13, 2022
Merged

Extend IsMatchFormatter, add docs to rc.2#245
axunonb merged 1 commit intoaxuno:version/v3.0from
axunonb:pr-extended-ismatchformatter

Conversation

@axunonb
Copy link
Copy Markdown
Member

@axunonb axunonb commented Feb 13, 2022

Advanced features for IsMatchFormatter (#245)

The IsMatchFormatter is a formatter with evaluation of regular expressions.

New: The formatter can output matching group values of a RegEx.

Example:

We'll evalute this argument with IsMatchFormatter:

KeyValuePair<string, object> arg = new("theValue", "Some123Content");
a) Simple "match or no match" distinction:

This behavior is unchanged.

_ = Smart.Format("{theValue:ismatch(^.+123.+$):Okay - {}|No match content}", arg);
// Result: "Okay - Some123Content"

_ = Smart.Format("{theValue:ismatch(^.+999.+$):Okay - {}|No match content}", arg);
// Result: "No match content"
b) Show the matches in the output:
// List the matching RegEx group values
_ = Smart.Format("{theValue:ismatch(^.+\\(1\\)\\(2\\)\\(3\\).+$):Matches for '{}'\\: {m:list:| - }|No match}", arg);
// Result: "Matches for 'Some123Content': Some123Content - 1 - 2 - 3"

// Show specific matching RegEx 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);
// Result: "First 2 matches in 'Some123Content': 1 and 2"

The placeholder m is for the collection of matching RegEx group values generated by IsMatchFormatter. The collection has at least one entry for a successful match. See more details in the Microsoft docs for the GroupCollection class.

The name of the placeholder can be set with IsMatchFormatter.PlaceholderNameForMatches. "m" is the default.

@axunonb axunonb merged commit 2d28e7c into axuno:version/v3.0 Feb 13, 2022
@axunonb axunonb deleted the pr-extended-ismatchformatter branch February 13, 2022 23:07
axunonb added a commit to axunonb/SmartFormat that referenced this pull request Mar 10, 2022
Add corresponding docs, too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant