Regex to remove string from string
19,096
Solution 1
Just use String.Replace()
String.Replace(".zip.ytu", "");
You don't need regex for exact matches.
Solution 2
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
Solution 3
txt = txt.Replace(".zip.ytu", "");
Why don't you simply do above?
Solution 4
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());
Solution 5
use string.Replace:
txt = txt.Replace(".zip.ytu", "");
Author by
user570715
Updated on June 30, 2022Comments
-
user570715 almost 2 years
Is there a regex pattern that can remove
.zip.ytu
from the string below?werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222
-
ΩmegaMan over 12 years[^.]* (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
-
Dmitry Frank over 12 yearsI 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?
-
Richard Moore about 3 yearsOP 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.
-
Richard Moore about 3 yearsThis is the actual answer to the question. 👍
-
Brian Booth over 2 yearsThis is the solution I was looking for. I'm not looking for exact matches, but regex pattern removal.
-
Mark Odey about 2 yearsSometimes you don't have javascript to fix the problem.