Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to remove string from string

Tags:

c#

regex

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

werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222
like image 423
user570715 Avatar asked Dec 27 '11 19:12

user570715


3 Answers

Just use String.Replace()

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

You don't need regex for exact matches.

like image 51
Mathletics Avatar answered Oct 03 '22 06:10

Mathletics


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
like image 34
ΩmegaMan Avatar answered Oct 03 '22 06:10

ΩmegaMan


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

Why don't you simply do above?

like image 43
Haris Hasan Avatar answered Oct 03 '22 07:10

Haris Hasan