5

Is there a regex pattern that can remove .zip.ytu from the string below?

werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222

6 Answers 6

12

Here is an answer using regex as the OP asked.

To use regex, put the replacment text in a match ( ) and then replace that match with nothing string.Empty:

string text = @"werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222";
string pattern = @"(\.zip\.ytu)";

Console.WriteLine( Regex.Replace(text, pattern, string.Empty ));

// Outputs 
// werfds_tyer.abc_20111223170226_20111222.20111222
Sign up to request clarification or add additional context in comments.

2 Comments

This is the actual answer to the question. 👍
This is the solution I was looking for. I'm not looking for exact matches, but regex pattern removal.
9

Just use String.Replace()

String.Replace(".zip.ytu", ""); 

You don't need regex for exact matches.

2 Comments

OP asked for a solution involving a regex. Years later I come here looking for a solution with a regex. You are correct but this doesn't answer OPs or my question.
Sometimes you don't have javascript to fix the problem.
2

Don't really know what is the ".zip.ytu", but if you don't need exact matches, you might use something like that:

string txt = "werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222";

Regex mRegex = new Regex(@"^([^.]*\.[^.]*)\.[^.]*\.[^_]*(_.*)$");
Match mMatch = mRegex.Match(txt);

string new_txt = mRegex.Replace(txt, mMatch.Groups[1].ToString() + mMatch.Groups[2].ToString());

2 Comments

[^.]* (Zero to unlimited) of not a period. Why kill the regex parser with the '*', specifically as the zero condition being valid? Instead, use of the '+' saying -1- to many would provide a better hint and not cause back tracking. Do you really believe that there will be nothing followed by a period; or do you believe that at least 1 character will exist? If you believe that 1 character will exist, then use that and not the *. HTH
I wrote that i don't really know what is the ".zip.ytu", and, well, i don't know what the whole string is. So, i could admit that this string might be something like "..test1.test2_123123.123123", because of why not? If i really know that there always should be something between these dots, then, of course, i would use "+" instead of "*". What's wrong?
1
txt = txt.Replace(".zip.ytu", "");

Why don't you simply do above?

Comments

0

use string.Replace:

txt = txt.Replace(".zip.ytu", "");

Comments

0

Here is the method I use for more complex repaces. Check out the link: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace(v=vs.110).aspx for a Regular Expression replace. I added the code below as well.

  string input = "This is   text with   far  too   much   " + 
                 "whitespace.";
  string pattern = "\\s+";
  string replacement = " ";
  Regex rgx = new Regex(pattern);
  string result = rgx.Replace(input, replacement);

  Console.WriteLine("Original String: {0}", input);
  Console.WriteLine("Replacement String: {0}", result);                             

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.