Gopal SA
Gopal SA

Reputation: 959

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. i.e 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)

Upvotes: 1

Views: 596

Answers (2)

Federico Piazza
Federico Piazza

Reputation: 30995

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)

enter image description here

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

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

Upvotes: 0

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391396

You can use this regular expression code:

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

.NET Fiddle example

Upvotes: 1

Related Questions