Skip to content

Commit 317f3cf

Browse files
committed
Add some Regex extension methods
1 parent c6be399 commit 317f3cf

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// (c) 2019 SharpCoding
2+
// This code is licensed under MIT license (see LICENSE.txt for details)
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using SharpCoding.SharpHelpers;
5+
6+
namespace SharpHelpers.UnitTest.Regex
7+
{
8+
[TestClass]
9+
public class RegexTests
10+
{
11+
[TestMethod]
12+
public void IsMatch_ShouldReturnTrue_WhenPatternMatches()
13+
{
14+
// Arrange
15+
string input = "hello world";
16+
string pattern = @"hello";
17+
18+
// Act
19+
bool result = input.IsMatchRegex(pattern);
20+
21+
// Assert
22+
Assert.IsTrue(result, "Expected the pattern to match the input string.");
23+
}
24+
25+
[TestMethod]
26+
public void IsMatch_ShouldReturnFalse_WhenPatternDoesNotMatch()
27+
{
28+
// Arrange
29+
string input = "hello world";
30+
string pattern = @"goodbye";
31+
32+
// Act
33+
bool result = input.IsMatchRegex(pattern);
34+
35+
// Assert
36+
Assert.IsFalse(result, "Expected the pattern not to match the input string.");
37+
}
38+
39+
[TestMethod]
40+
public void Match_ShouldReturnFirstMatch_WhenPatternMatches()
41+
{
42+
// Arrange
43+
string input = "hello world";
44+
string pattern = @"hello";
45+
46+
// Act
47+
string result = input.Match(pattern);
48+
49+
// Assert
50+
Assert.AreEqual("hello", result, "Expected the first match to be 'hello'.");
51+
}
52+
53+
[TestMethod]
54+
public void Match_ShouldReturnNull_WhenPatternDoesNotMatch()
55+
{
56+
// Arrange
57+
string input = "hello world";
58+
string pattern = @"goodbye";
59+
60+
// Act
61+
string result = input.Match(pattern);
62+
63+
// Assert
64+
Assert.IsNull(result, "Expected no match to be found.");
65+
}
66+
67+
[TestMethod]
68+
public void Replace_ShouldReplaceAllOccurrences_WhenPatternMatches()
69+
{
70+
// Arrange
71+
string input = "hello world world";
72+
string pattern = @"world";
73+
string replacement = "universe";
74+
75+
// Act
76+
string result = input.Replace(pattern, replacement);
77+
78+
// Assert
79+
Assert.AreEqual("hello universe universe", result, "Expected 'world' to be replaced with 'universe'.");
80+
}
81+
82+
[TestMethod]
83+
public void Split_ShouldReturnArray_WhenPatternIsUsedAsDelimiter()
84+
{
85+
// Arrange
86+
string input = "hello world universe";
87+
string pattern = @" ";
88+
89+
// Act
90+
string[] result = input.Split(pattern);
91+
92+
// Assert
93+
CollectionAssert.AreEqual(new[] { "hello", "world", "universe" }, result, "Expected the input string to be split by spaces.");
94+
}
95+
}
96+
}

SharpHelpers/SharpHelpers/RegexHelper.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// (c) 2019 SharpCoding
22
// This code is licensed under MIT license (see LICENSE.txt for details)
3+
using System;
34
using System.Text.RegularExpressions;
45

56
namespace SharpCoding.SharpHelpers
@@ -14,8 +15,56 @@ public static class RegexHelper
1415
/// <returns></returns>
1516
public static bool IsMatchRegex(this string value, string pattern)
1617
{
18+
if (value == null) throw new ArgumentNullException(nameof(value));
19+
if (pattern == null) throw new ArgumentNullException(nameof(pattern));
20+
1721
var regex = new Regex(pattern);
18-
return (regex.IsMatch(value));
22+
return regex.IsMatch(value);
23+
}
24+
25+
/// <summary>
26+
/// Retrieves the first match found in the input string.
27+
/// </summary>
28+
/// <param name="input">The input string to search.</param>
29+
/// <param name="pattern">The regular expression pattern.</param>
30+
/// <returns>The first match found, or null if no match is found.</returns>
31+
public static string Match(this string input, string pattern)
32+
{
33+
if (input == null) throw new ArgumentNullException(nameof(input));
34+
if (pattern == null) throw new ArgumentNullException(nameof(pattern));
35+
36+
var match = Regex.Match(input, pattern);
37+
return match.Success ? match.Value : null;
38+
}
39+
40+
/// <summary>
41+
/// Replaces all occurrences of a pattern with a replacement string.
42+
/// </summary>
43+
/// <param name="input">The input string to modify.</param>
44+
/// <param name="pattern">The regular expression pattern.</param>
45+
/// <param name="replacement">The replacement string.</param>
46+
/// <returns>A new string with all occurrences replaced.</returns>
47+
public static string Replace(this string input, string pattern, string replacement)
48+
{
49+
if (input == null) throw new ArgumentNullException(nameof(input));
50+
if (pattern == null) throw new ArgumentNullException(nameof(pattern));
51+
if (replacement == null) throw new ArgumentNullException(nameof(replacement));
52+
53+
return Regex.Replace(input, pattern, replacement);
54+
}
55+
56+
/// <summary>
57+
/// Splits a string into an array of substrings using a regex pattern as a delimiter.
58+
/// </summary>
59+
/// <param name="input">The input string to split.</param>
60+
/// <param name="pattern">The regular expression pattern to use as a delimiter.</param>
61+
/// <returns>An array of substrings.</returns>
62+
public static string[] Split(this string input, string pattern)
63+
{
64+
if (input == null) throw new ArgumentNullException(nameof(input));
65+
if (pattern == null) throw new ArgumentNullException(nameof(pattern));
66+
67+
return Regex.Split(input, pattern);
1968
}
2069
}
2170
}

0 commit comments

Comments
 (0)