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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:48:34+00:00 2026-06-01T22:48:34+00:00

I am trying to add a bracket to operands of OR operator. For example,

  • 0

I am trying to add a bracket to operands of OR operator. For example, if I have statement as below,

C>O AND C>4 OR C>0 AND C>5

I would like to format it as below

(C>O AND C>4) OR (C>0 AND C>5)

I wrote a simple code to do this as below. However, if the string has more than one OR statements the code doesn’t work properly. I was told that the regular expression could accomplish this. But I have very minimum knowledge in regular expression.

string mystring = "C>O AND C>4 OR C>0 AND C>5";
int indexFound = mystring.IndexOf("OR");
string left = mystring.Substring(0, indexFound - 1);
string right = mystring.Substring(indexFound + 2, mystring.Length - (indexFound + 2));
string output = "(" + left + ")" + "OR" + "(" + right + ")";

Any help would be appreciated.

  • 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-06-01T22:48:35+00:00Added an answer on June 1, 2026 at 10:48 pm

    Here is the way via regular expressions:

    static string AddBracketsToOr(string input)
    {            
        Regex regex = new Regex(@"(?<left>.+[AND|OR].+) OR (?<right>.+[AND|OR].+)");
        Match match = regex.Match(input);
    
        if (match == null)
            throw new Exception("Wrong input format");
    
        return String.Format("({0}) OR ({1})", 
                             match.Groups["left"], match.Groups["right"]);
    }
    

    Usage:

    var result = AddBracketsToOr("C>O AND C>4 OR C>0 AND C>5");
    

    UPDATE:
    Works with

    "C>O AND C>4 OR C>0 AND C>5" // (C>O AND C>4) OR (C>0 AND C>5)
    "C>9 OR C>O AND C>4 OR C>0 AND C>5" // (C>9 OR C>O AND C>4) OR (C>0 AND C>5)
    "C>9 OR C>O OR C>0 AND C>5" // (C>9 OR C>O) OR (C>0 AND C>5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying add a tab to my web page that looks like this: Using
I'm trying add a space before a particular string ( Token for example) by
I'm trying add some implicit rules in CMake that will generate direct dependencies (like
I'm trying add some code at run time to the head element. When I
I'm trying to match a regular expression in Perl. My code looks like the
I am trying add OR operator in awk command. The following does not work.
I'm trying add vertical lines to my grid. I have found some examples but
Should be a relatively simple question -- I'm trying to add a css class
Trying to add a 'box' to a form at design time, I looked up
Trying to add email notification to my app in the cleanest way possible. When

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.