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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:23:27+00:00 2026-05-13T13:23:27+00:00

Is there a single regular expression that can parse a string (in Python and/or

  • 0

Is there a single regular expression that can parse a string (in Python and/or Javascript, does not need to be the same expression) that represents simple boolean arithmetic? For example I want to parse this string:

a and (b and c) and d or e and (f or g)

Assuming that:
* parentheses do not nest
* the terms a, b, …, z are not sub-expressions

The resulting captures should be grouped by parentheses first, which I then parse again with the same or a simpler regex.

I’ve had success writing a naive regex for parsing boolean arithmetic without parentheses.

Any ideas?

  • 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-13T13:23:27+00:00Added an answer on May 13, 2026 at 1:23 pm

    Normally you would use for example a recursive descent parser for this task, but you can grab all the parts (tokens) with a regex:

    x = 'a and (b and c) and d or e and (f or g)'
    import re
    
    matches = re.findall(r'\(.*?\)|\w+', x)
    print ','.join(matches)
    

    The operators usually have different precedence. Parentheses would be evaluated first, then and expressions, and finally or expressions, with left-to-right order in case of equal precedence. You say you want to return the parentheses matches first, but actually what you would normally do is to use the parts build an expression tree and evalulate that recursively.

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

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer for i in growthRates: fund = fund * (1 +… May 16, 2026 at 1:56 pm
  • Editorial Team
    Editorial Team added an answer For my own framework projects, typically monitoring, testing, or in-page… May 16, 2026 at 1:56 pm
  • Editorial Team
    Editorial Team added an answer setvbuf would be the obvious place to start. May 16, 2026 at 1:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is there a way to apply a rule to a single directory and not
Is there anyway i can convert the below 3 sql queries into a single
I am building a 'keyword' highlighting script and I need to write a regular
I have a pascal code file and need to parse it (using c#) and
I am hoping that there might be an easy way to do this, I
I'm trying to parse a DSN (from a Symfony application) using regular expressions, in
Executive Summary: preg_replace() ran faster than string comparisons. Why? Shouldn't regular expressions be slower?
I'm trying to create a regular expression for matching latitude/longitude coordinates. For matching a
There is a task of managing desktop client session status: Ensure only single client
OK, I need to scan many HTML / XHTML documents to see if a

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.