1

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.

Yaroslav Yakovlev
  • 6,303
  • 6
  • 39
  • 59
  • 3
    how can you tell when the function starts and ends? Can functions be nested? Can you explain your grammar a bit more? – tster May 22 '14 at 08:38
  • IgnoreFunction is a regular expression host. So it can be any regex inside it in single or double quotes. AlsoIgnoreFunction have a list of string params that go one by one and are comma separated. – Yaroslav Yakovlev May 22 '14 at 08:59
  • This situation sounds straight out of [Match (or replace) a pattern except in situations s1, s2, s3 etc](http://stackoverflow.com/questions/23589174/match-or-replace-a-pattern-except-in-situations-s1-s2-s3-etc/23589204#23589204), have a look. You'll need to define some simple regex for the things you want to exclude. The question also has a linked article that has sample C# code to do it. – zx81 May 22 '14 at 10:41
  • Checked it and regexegg article. But it seems everything is picked, just in different groups. Is that my case for sure? I dont want everything to be picked,even if its in different groups. – Yaroslav Yakovlev May 22 '14 at 11:16
  • Updated the question. See example I tried, based on your answer and that article. For some reason everything is matched, in different groups but it match it all still. – Yaroslav Yakovlev May 22 '14 at 11:20
  • I still don't understand how IgnoreFunction works. You say the expression is between quotes, but in your question, there is never a closing quote, for instance `IgnoreFunction 'SomeVar=AnotherVar` is open, there is no closing `'`. Is that correct? – zx81 May 30 '14 at 10:39

1 Answers1

1

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...

Community
  • 1
  • 1
zx81
  • 41,100
  • 9
  • 89
  • 105