RegexMatchExtensions class

Extension methods for extracting values from regular expression matches.

public static class RegexMatchExtensions

Public Members

name description
static Get<T>(…) Returns a value of the specified type for the match. (2 methods)
static TryGet<T>(…) Attempts to return a value of the specified type for the match. (4 methods)

Remarks

These methods convert successful matches into values of the specified type.

For simple types, the text of the first capturing group is converted to that type; if there are no capturing groups, the entire match is converted.

For tuple types, each capturing group is converted to an item in the requested tuple.

If a group is not successful, the default value is returned, which is null for nullable types, but zero for non-nullable numeric types.

If a group has multiple captures and the target type is an array, each capture is converted to an item in the returned array.

If the target type is string, the text of the group/capture is returned.

If the target type is bool, true is returned (unless the group was not successful, per above).

If the target type is a numeric type or Guid, the text of the group/capture is parsed into that type using the invariant culture and default settings, which allow leading and trailing whitespace. If the text is empty or only whitespace and the type is nullable, null is returned. If the text cannot be parsed into that type, the corresponding FormatException is thrown.

If the target type is an enumerated type, the text of the group/capture is parsed as that type, ignoring case.

If the target type is Group or Capture, the corresponding object of that type for the group/capture is returned.

If an unsupported type is used, InvalidOperationException is thrown.

See Also