I am trying to find, and remove, a specific pattern inside a string with C#.
The pattern is an asterisk, followed by any number of numbers, followed by .txt
Example strings:
Given these examples, the desired results would be:
How can this be accomplished?
string pattern = @"\*\d*\.txt";
Regex rgx = new Regex(pattern)
input = rgx.Replace(input, "");
If you build a regular expression and replace its matches with an empty string, you're effectively removing that pattern. Here's what you'll need for your pattern:
An asterisk has a special meaning in a regular expression (zero or more of the previous item), so you'll have to escape it with a backslash (\*).
You can match a digit with the digit character class (\d) or with an explicit class that includes all of them ([0-9]). There are differences between them because of culture settings: \d can match things like eastern arabic numerals (٠.١.٢.٣.٤.٥.٦.٧.٨.٩), while [0-9] will match only the hindu-arabic numerals (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
You can use a + quantifier to match one or more of the previous item: \d+ will match one or more digits.
A dot is another special character (it matches any single character except for newlines). It will also need escaping (\.).
You can match text without special characters with the text itself: txt matches exactly txt.
Putting everything together we get:
string purged = Regex.Replace(input, @"\*[0-9]+\.txt", "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With