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

  • SEARCH
  • Home
  • 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 592157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:42:36+00:00 2026-05-13T15:42:36+00:00

I’m a regex newbie and need a single expression that: matches the an and

  • 0

I’m a regex newbie and need a single expression that:

matches the “an” and the “AN” but not the “and” or “AND” and matches the “o” and the “O” but not the “or” or “OR” in this predicate:

1and(2or3)AND(4OR5)an(6o7)AN(8O9)

Basically I can’t figure out how to convert the expression:

var myRegEx = Regex("[0-9 ()]|AND|OR")

into a “everything but”, case insensitive expression.

Can’t use the regex word boundaries feature because the predicate isn’t required to have spaces.

(Added after two answers were already
provided): I also need to know the
index of the match, which is why I’m
assuming I need to use the
Regex.Match() method.

Thanks!

Here’s what I ended up with:

  private bool mValidateCharacters()
  {
     const string legalsPattern = @"[\d ()]|AND|OR";
     const string splitPattern = "(" + legalsPattern + ")";
     int position = 0;
     string[] tokens = Regex.Split(txtTemplate.Text, splitPattern, RegexOptions.IgnoreCase);

     // Array contains every legal operator/symbol found in the entry field
     // and every substring preceeding, surrounded by, or following those operators/symbols
     foreach (string token in tokens)
     {
        if (string.IsNullOrEmpty(token))
        {
           continue;
        }

        // Determine if the token is a legal operator/symbol or a syntax error
        Match match = Regex.Match(token, legalsPattern, RegexOptions.IgnoreCase);

        if (string.IsNullOrEmpty(match.ToString()))
        {
           const string reminder =
              "Please use only the following in the template:" +
              "\n\tRow numbers from the terms table" +
              "\n\tSpaces" +
              "\n\tThese characters: ( )" +
              "\n\tThese words: AND OR";
           UserMsg.Tell("Illegal template entry '" + token + "'at position: " + position + "\n\n" + reminder, UserMsg.EMsgType.Error);
           txtTemplate.Focus();
           txtTemplate.Select(position, token.Length);
           return false;
        }

        position += token.Length;
     }

     return true;
  }
  • 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-13T15:42:36+00:00Added an answer on May 13, 2026 at 3:42 pm

    Randal Schwartz‘s rule: Use capturing in Regex.Match when you know what you want to keep, and use Regex.Split when you know what you want to throw away.

    You wrote you want “everything but,” so

    var input = "1and(2or3)AND(4OR5)an(6o7)AN(8O9)";
    foreach (var s in Regex.Split(input, @"[\d()]|AND|OR", RegexOptions.IgnoreCase))
      if (s.Length > 0)
        Console.WriteLine("[{0}]", s);
    

    Output:

    [an]
    [o]
    [AN]
    [O]

    To get the offsets, save the separators by enclosing the regular expression in parentheses:

    var input = "1and(2or3)AND(4OR5)an(6o7)AN(8O9)";
    string pattern = @"([\d()]|AND|OR)";
    int offset = 0;
    foreach (var s in Regex.Split(input, pattern, RegexOptions.IgnoreCase)) {
      if (s.ToLower() == "an" || s.ToLower() == "o")
        Console.WriteLine("Found [{0}] at offset {1}", s, offset);
      offset += s.Length;
    }
    

    Output:

    Found [an] at offset 19
    Found [o] at offset 23
    Found [AN] at offset 26
    Found [O] at offset 30
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.