简体   繁体   中英

C# Regular Expression Reversing Match

I am looking to convert a part of a string which is substringof('has',verb) into contains(verb,'has')

As you can see, what is changing is just substring to contains and the two parameters passed to the function reversed.

I am looking for a generic solution, by using regex. Preferably using tags. ie once i get two matches, i need to be able to reverse the matches by using $2$1 (This is how i remember doing this in perl)

You can use this regular expression code:

var re = new Regex(@"substringof\('([^']+)',([^)]+)\)");
string output = re.Replace(input, @"contains($2, '$1')");

.NET Fiddle example

You can use a regex like this:

.*?\((.*?),(.*?)\)

Working demo

Then you can use a string replacement like this:

contains(\2,\1)    or
contains($2,$1)

在此处输入图片说明

Btw, if you just want to change the substringof , then you can use:

substringof\((.*?),(.*?)\)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM