Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a pattern from a string using Regex

Tags:

c#

regex

replace

I want to find paths from a string and remove them e.g. string1 = "'c:\a\b\c'!MyUDF(param1, param2,..) + 'c:\a\b\c'!MyUDF(param3, param4,..)...", I'd like a regex to find the pattern '[some path]'!MyUDF, and remove '[path]'. Thanks

Edit e.g. input string1 ="'c:\a\b\c'!MyUDF(param1, param2,..) + 'c:\a\b\c'!MyUDF(param3, param4,..)"; expected output "MyUDF(param1, param2,...) + MyUDF(param3, param4,...)" where MyUDF is a function name, so it consists of only letters

like image 651
toosensitive Avatar asked Oct 24 '13 17:10

toosensitive


2 Answers

input=Regex.Replace(input,"'[^']+'(?=!MyUDF)","");

In case if the path is followed by ! and some other word you can use

input=Regex.Replace(input,@"'[^']+'(?=!\w+)","");
like image 119
Anirudha Avatar answered Sep 18 '22 06:09

Anirudha


Alright, if the ! is always in the string as you suggest, this Regex !(.*)?\( will get you what you want. Here is a Regex 101 to prove it.

To use it, you might do something like this:

var result = Regex.Replace(myString, @"!(.*)?\(");
like image 34
Mike Perrenoud Avatar answered Sep 21 '22 06:09

Mike Perrenoud