RegEx to match a pattern unless preceded by another pattern

I need a regular expression that should be used to replace text. Example: text to be matched ABC(which may be surrounded by square brackets), wildcard text DEF. This is quite thorough. The complication is that I do not want to match the text ABCwhen it is preceded by a pattern \[[\d ]+\]\.- in other words, when it is preceded by a word or a set of words in brackets, followed by a period.

Here are some examples of the source text that you want to map, and the result after replacing the regular expression:

1. [xxx xxx].[ABC] > [xxx xxx].[ABC] (does not match - first part fits the pattern)
2. [xxx xxx].ABC   > [xxx xxx].ABC   (does not match - first part fits the pattern)
3. [xxx.ABC        > [xxx.DEF        (matches - first part has no closing bracket)
4. [ABC]           > [DEF]           (matches - no first part)
5. ABC             > DEF             (matches - no first part)
6. [xxx][ABC]      > [xxx][DEF]      (matches - no period in between)
7. [xxx]. [ABC]    > [xxx] [DEF]     (matches - space in between)

What is the reason for this: how can I indicate the previous template, which, if present, will prevent a match? What will be the pattern in this case? (C # regular expression flavor)

+5
source share
1 answer

You want an expression of negative appearance. They look like (?<!pattern), therefore:

(?<!\[[\d ]+\]\.)\[?ABC\]?

Note that this does not force a matching pair of square brackets around ABC; it just allows you to use the optional open bracket before and after that close the brackets. If you want to force pair matching or not, you will have to use alternation:

(?<!\[[\d ]+\]\.)(?:ABC|\[ABC\])

. ABC, .

ETA: , , , , , ABC], . [ , . , [ "", :

(?<!\[[\d ]+\]\.\[?)ABC\]?

, :

[123].[ABC]: fail (expected: fail)
[123 456].[ABC]: fail (expected: fail)
[123.ABC: match (expected: match)
    matched: ABC
ABC: match (expected: match)
    matched: ABC
[ABC]: match (expected: match)
    matched: ABC]
[ABC[: match (expected: fail)
    matched: ABC

[ ], , , , , :

(?:(?<!\[[\d ]+\]\.\[)ABC\]|(?<!\[[\d ]+\]\.)(?<!\[)ABC(?!\]))

, :

[123].[ABC]: fail (expected: fail)
[123 456].[ABC]: fail (expected: fail)
[123.ABC: match (expected: match)
    matched: ABC
ABC: match (expected: match)
    matched: ABC
[ABC]: match (expected: match)
    matched: ABC]
[ABC[: fail (expected: fail)

:

// Compile and run with: mcs so_regex.cs && mono so_regex.exe
using System;
using System.Text.RegularExpressions;

public class SORegex {
  public static void Main() {
    string[] values = {"[123].[ABC]", "[123 456].[ABC]", "[123.ABC", "ABC", "[ABC]", "[ABC["};
    string[] expected = {"fail", "fail", "match", "match", "match", "fail"};
    string pattern = @"(?<!\[[\d ]+\]\.\[?)ABC\]?";  // Don't force [ to match ].
    //string pattern = @"(?:(?<!\[[\d ]+\]\.\[)ABC\]|(?<!\[[\d ]+\]\.)(?<!\[)ABC(?!\]))";  // Force balanced brackets.
    Console.WriteLine("pattern: {0}", pattern);
    int i = 0;
    foreach (string text in values) {
      Match m = Regex.Match(text, pattern);
      bool isMatch = m.Success;
      Console.WriteLine("{0}: {1} (expected: {2})", text, isMatch? "match" : "fail", expected[i++]);
      if (isMatch) Console.WriteLine("\tmatched: {0}", m.Value);
    }
  }
}
+11

All Articles