English 中文(简体)
Is it possible to negate a regular expression search?
原标题:
  • 时间:2009-11-09 19:00:36
  •  标签:
  • c#
  • regex

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This allows the engine to lex all sort of fun and interesting things and output a tokenised file.

One of the issues im having is I want the user to have everything contained in this tokenised file. I.E the parts they are looking for and the parts they are not (Partial Highlighting would be a good example of this).

Based on the way my lexer highlights I found the best way to do this would be to negate the regular expressions given by the user.

So if the user wanted to lex a string for every occurrence of "T" the negated version would find everything except "T".

Now the above is easy to do but what if a user supplies 8 different expressions of a complex nature, is there a way to put all these expressions into one and negate the lot?

最佳回答

You could combine several RegEx s into 1 by using (pattern1)|(pattern1)|... To negate it you just check for !IsMatch

var matches = Regex.Matches("aa bb cc dd", @"(?<token>a{2})|(?<token>d{2})"); 

would return in fact 2 tokens (note that I ve used the same name twice.. that s ok) Also explore Regex.Split. For instance:

var split = Regex.Split("aa bb cc dd", @"(?<token>aa bb)|(?:s+)");

returns the words as tokens, except for "aa bb" which is returned as one token because I defined it as so with (?...).

You can also use the Index and Length properties to calculate the middle parts that have not been recognized by the Regex:

var matches = Regex.Matches("aa bb cc dd", @"(?<token>a{2})|(?<token>d{2})");
for (int i = 0; i < matches.Count; i++)
{
   var group = matches[i].Groups["token"];
   Console.WriteLine("Token={0}, Index={1}, Length={2}", group.Value, group.Index, group.Length);
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签