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 7621289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:09:38+00:00 2026-05-31T04:09:38+00:00

I have a Regex to split out words operators and brackets in simple logic

  • 0

I have a Regex to split out words operators and brackets in simple logic statements (e.g. “WORD1 & WORD2 | (WORd_3 & !word_4 )”. the Regex I’ve come up with is “(?[A-Za-z0-9_]+)|(?[&!\|()]{1})”. Here is a quick test program.


using System; 
using System.Text.RegularExpressions;

namespace ConsoleApplication1 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
        Console.WriteLine("* Test Project *"); 
        string testExpression = "!(LIONV6 | NOT_superCHARGED) &RHD"; 
        string removedSpaces = testExpression.Replace(" ", ""); 
        string[] expectedResults = new string[] { "!", "(", "LIONV6", "|", "NOT_superCHARGED", ")", "&", "RHD" }; 
        string[] splits = Regex.Split(removedSpaces, @"(?[A-Za-z0-9_]+)|(?[&!\|()]{1})");

        Console.WriteLine("Expected\n{0}\nActual\n{1}", expectedResults.AllElements(), splits.AllElements());

        Console.WriteLine("*** Any Key to finish ***");
        Console.ReadKey();
    }
}

public static class Extensions
{
    public static string AllElements(this string[] str)
    {
        string output = "";
        if (str != null)
        {
            foreach (string item in str)
            {
                output += "'" + item + "',";
            }
        }
        return output;
    }
}

The Regex does the required job of splitting out words and operators into an array in the right sequence, but the result array contains many empty elements, and I can’t work out why. Its not a serious problem as I just ignore empty elements when consuming the array but I’d like Regex to do all the work if possible, including ignoring spaces.

  • 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-31T04:09:39+00:00Added an answer on May 31, 2026 at 4:09 am
    var matches = Regex.Matches(removedSpaces, @"(\w+|[&!|()])");
    
    foreach (var match in matches)
        Console.Write("'{0}', ", match); // '!', '(', 'LIONV6', '|', 'NOT_superCHARGED', ')', '&', 'RHD', 
    

    Actually, you don’t need to delete spaces before extracting your identifiers and operators, the regex I proposed will ignore them anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string: [\n['-','some text what\rcontains\nnewlines'],\n\n trying to parse: Regex.Split(@[\n['-','some text what contains
I have tried this... Dim myMatches As String() = System.Text.RegularExpressions.Regex.Split(postRow.Item(Post), \b\#\b) But it is
I have a regex expression that I'm doing a split against another string and
I have simple regex \.*\ for me its says select everything between and ,
C# Regex Split - commas outside quotes var result = Regex.Split(samplestring, ,(?=(?:[^\]*\[^\]*')*[^\]*$)); I have
I have a regex that was written for me for passwords: ~^[a-z0-9!@#\$%\^&\*\(\)]{8,16}$~i It's supposed
I have recently figured out that I haven't been using regex properly in my
I have created a function to implement the regex.split in sql. Here's the code:
I have been trying to figure out a Java RegEx for some while now
I have a regex that is going to end up being a bit long

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.