|
1 | 1 | using System; |
2 | 2 | using System.Diagnostics.Contracts; |
| 3 | +using System.Text.RegularExpressions; |
3 | 4 |
|
4 | 5 | namespace Open.Text |
5 | 6 | { |
@@ -44,6 +45,26 @@ public static StringSegment First(this string source, string search, StringCompa |
44 | 45 | return i == -1 ? default : StringSegment.Create(source, i, search.Length); |
45 | 46 | } |
46 | 47 |
|
| 48 | + /// <summary> |
| 49 | + /// Finds the first instance of a pattern and returns a StringSegment for subsequent use. |
| 50 | + /// </summary> |
| 51 | + /// <param name="source">The source string to search.</param> |
| 52 | + /// <param name="pattern">The pattern to look for.</param> |
| 53 | + /// <returns> |
| 54 | + /// The segment representing the found string. |
| 55 | + /// If not found, the StringSegment.IsValid property will be false. |
| 56 | + /// </returns> |
| 57 | + /// <remarks>If the pattern is right-to-left, then it will return the first segment from the right.</remarks> |
| 58 | + public static StringSegment First(this string source, Regex pattern) |
| 59 | + { |
| 60 | + if (source is null) throw new ArgumentNullException(nameof(source)); |
| 61 | + if (pattern is null) throw new ArgumentNullException(nameof(pattern)); |
| 62 | + Contract.EndContractBlock(); |
| 63 | + |
| 64 | + var match = pattern.Match(source); |
| 65 | + return match.Success ? StringSegment.Create(source, match.Index, match.Length) : default; |
| 66 | + } |
| 67 | + |
47 | 68 | /// <inheritdoc cref="First(string, string, StringComparison)" /> |
48 | 69 | public static StringSegment First(this StringSegment source, in ReadOnlySpan<char> search, StringComparison comparisonType = StringComparison.Ordinal) |
49 | 70 | { |
@@ -83,6 +104,23 @@ public static StringSegment Last(this string source, string search, StringCompar |
83 | 104 | return i == -1 ? default : StringSegment.Create(source, i, search.Length); |
84 | 105 | } |
85 | 106 |
|
| 107 | + /// <inheritdoc cref="First(string, Regex)"/> |
| 108 | + /// <summary> |
| 109 | + /// Finds the last instance of a pattern and returns a StringSegment for subsequent use. |
| 110 | + /// </summary> |
| 111 | + /// <remarks>If the pattern is right-to-left, then it will return the last segment from the right (first segment from the left).</remarks> |
| 112 | + public static StringSegment Last(this string source, Regex pattern) |
| 113 | + { |
| 114 | + if (source is null) throw new ArgumentNullException(nameof(source)); |
| 115 | + if (pattern is null) throw new ArgumentNullException(nameof(pattern)); |
| 116 | + Contract.EndContractBlock(); |
| 117 | + |
| 118 | + var matches = pattern.Matches(source); |
| 119 | + if (matches.Count == 0) return default; |
| 120 | + var match = matches[matches.Count - 1]; |
| 121 | + return StringSegment.Create(source, match.Index, match.Length); |
| 122 | + } |
| 123 | + |
86 | 124 | /// <inheritdoc cref="Last(string, string, StringComparison)" /> |
87 | 125 | public static StringSegment Last(this StringSegment source, in ReadOnlySpan<char> search, StringComparison comparisonType = StringComparison.Ordinal) |
88 | 126 | { |
|
0 commit comments