Regex to remove a string from a string

I wonder if I can write a regex to do below, String.Remove (17.7) is currently used

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

I want to remove .zip.ytu from the line above

+4
source share
6 answers

Just use String.Replace()

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

You do not need a regular expression for exact matches.

+9
source

Here is the answer using a regular expression, as the OP asked. ;-)

To use the regex, put the rewrite text in match (), and then replace this 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 

NTN

+4
source
 txt = txt.Replace(".zip.ytu", ""); 

Why don't you just do the above?

+3
source

use string.Replace:

 txt = txt.Replace(".zip.ytu", ""); 
+2
source

I don't know what ".zip.ytu" is, but if you don’t need exact matches, you can use something like this:

 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
source

Here is the method I use for more complex measurements. Check the link: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace(v=vs.110).aspx to replace the regex. I have added the code below.

  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); 
0
source

Source: https://habr.com/ru/post/1388187/


All Articles