Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3308964
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:35:02+00:00 2026-05-17T21:35:02+00:00

I need a regex that is to be used for text substitution. Example: text

  • 0

I need a regex that is to be used for text substitution. Example: text to be matched is ABC (which could be surrounded by square brackets), substitution text is DEF. This is basic enough. The complication is that I don’t want to match the ABC text when it is preceded by the pattern \[[\d ]+\]\. – in other words, when it is preceded by a word or set of words in brackets, followed by a period.

Here are some examples of source text to be matched, and the result, after the regex substitution would be made:

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 it comes down to is: how can I specify the preceding pattern that when present as described will prevent a match? What would the pattern be in this case? (C# flavor of regex)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:35:02+00:00Added an answer on May 17, 2026 at 9:35 pm

    You want a negative look-behind expression. These look like (?<!pattern), so:

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

    Note that this does not force a matching pair of square brackets around ABC; it just allows for an optional open bracket before and an optional close bracket after. If you wanted to force a matching pair or none, you’d have to use alternation:

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

    This uses non-capturing parentheses to delimit the alternation. If you want to actually capture ABC, you can of turn that into a capture group.

    ETA: The reason the first expression seems to fail is that it is matching on ABC], which is not preceded by the prohibited text. The open bracket [ is optional, so it just doesn’t match that. The way around this is to shift the optional open bracket [ into the negative look-behind assertion, like so:

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

    An example of what it matches and doesn’t:

    [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
    

    Trying to make the presence of an open bracket [ force a matching close bracket ], as the second pattern intended, is trickier, but this seems to work:

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

    An example of what it matches and doesn’t:

    [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)
    

    The examples were generated using this code:

    // 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);
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a Regex that will match a java method declaration. I have come
I need a regex that matches all strings ending in .cs, but if they
I need a Regex Statement (run in c#) that will take a string containing
I have a regex call that I need help with. I haven't posted my
I need to pass a regex substitution as a variable: sub proc { my
I need to find a reg ex that only allows alphanumeric. So far, everyone
I need a regex for the following pattern: Total of 5 characters (alpha and
What regex pattern would need I to pass to java.lang.String.split() to split a String
I need to find all the regex matches from a list of strings. For
I need to match (case insensitive) abcd and an optional trademark symbol Regex: /abcd(™)?/gi

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.