I am trying t开发者_运维知识库o 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:
- test*123.txt
- test2*1.txt
- test*1234.txt3
- test4*12.txt123
Given these examples, the desired results would be:
- test ("*123.txt" was removed)
- test2 ("*1.txt" was removed)
- test3 ("*1234.txt" was removed)
- test4123 ("*12.txt" was removed)
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 exactlytxt
.
Putting everything together we get:
string purged = Regex.Replace(input, @"\*[0-9]+\.txt", "");
I would use RegEx to solve this. I recommend an online editor to help you do this. It's called Rubular and can be found at http://www.rubular.com/
精彩评论