Yaroslav Yakovlev
Yaroslav Yakovlev

Reputation: 6483

c# regexp replace tokens, except ones in parentheses

I need to replace list of tokens, like ORELSE, ANDALSO, =, <>. But I only need to do it, when they are alone, not in a function. So

SomeVar ANDALSO SomeOTherVar ANDALSO AnotherVar = 1234

Should be replaced with

SomeVar && SomeOTherVar && AnotherVar == 1234.

Thats something I can do. But I need to ignore tokens that are inside some function, like

IgnoreFunction 'SomeVar=AnotherVar ANDALSO test = anotherTest. Or AlsoIgnoreFunction['test=value', 'anotherTest = anotherValue']

So expression SomeVar ANDALSO SomeOTherVar ANDALSO AnotherVar = 1234 IgnoreFunction 'SomeVar=AnotherVar Should be replaced by

SomeVar && SomeOTherVar && AnotherVar == 1234 IgnoreFunction 'SomeVar=AnotherVar

As I can match simple tokens and can match these ignore functions, I cant match tokens everywhere, except inside ignore functions.

For now I use the following regexp to match tokens: ANDALSO|ORELSE|=|<>

And this regexp to match everything inside ignore function: \bIgnoreFunction\b (["'])(\\?.)*?\1

But I cant to figure out the pattern to stop matching equality mark inside ignore functions.

Heres the regex I used to test it: TEST (["'])(\\?.)*?\1|(=|AND|OR) I tested it with that expression: Request.Query TEST '[\?&]th=mm(&|$)' = = = AND OR on this site: http://regexstorm.net/tester And it match everything, not only =, AND, OR tokens.

Upvotes: 1

Views: 130

Answers (1)

zx81
zx81

Reputation: 41838

Yaroslav, you say that IgnoreFunction is in quotes, but in your question the quotes are not closed, for instance in IgnoreFunction 'SomeVar=AnotherVar ANDALSO test = anotherTest. I'm going to assume this is a typo.

Also, you have said that you want to replace ANDALSO with &&. For illustration, I'm also going to assume that you want to replace ORELSE with ||

Here's our regex:

IgnoreFunction\s*(['"])[^']*\1|AlsoIgnoreFunction\[[^\]]*\]|(ANDALSO|ORELSE)

The left side of the alternation matches complete IgnoreFunction and AlsoIgnoreFunction expressions. We will ignore these matches. The right side matches and captures ANDALSO and ORELSE to Group 2, and we know they are the right ones because they were not matched by the expressions on the left.

This program shows how to use the regex (see the results at the bottom of the online demo):

using System;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
class Program
{
static void Main()  {
var myRegex = new Regex(@"IgnoreFunction\s*(['""])[^']*\1|AlsoIgnoreFunction\[[^\]]*\]|(ANDALSO|ORELSE)");
string s1 = @"SomeVar ANDALSO SomeOTherVar ANDALSO AnotherVar = 1234 IgnoreFunction 'SomeVar=ANDALSO AnotherVar'
AlsoIgnoreFunction['test=value', 'anotherTest = ANDALSO anotherValue'] ORELSE ANDALSO";

string replaced = myRegex.Replace(s1, delegate(Match m) {
    if (m.Groups[2].Value == "ANDALSO") return "&&";
    else if (m.Groups[2].Value == "ORELSE") return "||";
    else return m.Value;
    });
Console.WriteLine("\n" + "*** Replacements ***");
Console.WriteLine(replaced);


Console.WriteLine("\nPress Any Key to Exit.");
Console.ReadKey();

} // END Main
} // END Program

Reference

How to match (or replace) a pattern except in situations s1, s2, s3...

Upvotes: 1

Related Questions