5

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:

  1. test*123.txt
  2. test2*1.txt
  3. test*1234.txt3
  4. test4*12.txt123

Given these examples, the desired results would be:

  1. test ("*123.txt" was removed)
  2. test2 ("*1.txt" was removed)
  3. test3 ("*1234.txt" was removed)
  4. test4123 ("*12.txt" was removed)

How can this be accomplished?

2
  • You should look into the specifics of regular expressions. Here is a cheat sheet: mikesdotnetting.com/Article/46/… Commented Mar 2, 2011 at 20:32
  • Does any number of numbers include 0? Commented Mar 2, 2011 at 20:35

3 Answers 3

9
string pattern = @"\*\d*\.txt";
Regex rgx = new Regex(pattern)
input = rgx.Replace(input, "");
Sign up to request clarification or add additional context in comments.

Comments

8

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:

  1. 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 (\*).

  2. 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).

  3. You can use a + quantifier to match one or more of the previous item: \d+ will match one or more digits.

  4. A dot is another special character (it matches any single character except for newlines). It will also need escaping (\.).

  5. 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", "");

Comments

1

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.