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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:11:10+00:00 2026-06-04T12:11:10+00:00

I’m trying to write the correct regular expression in PHP to parse a string

  • 0

I’m trying to write the correct regular expression in PHP to parse a string (written by some user) to build a request. It can be as complex as :

name = 'benjo' and (surname = 'benny' or surname = 'bennie') or age = 4

Later I’ll parse the string to build mySQL queries. For now, I’m just trying to find the correct regular expression to parse this string into an array that could look like :

$result = array(
0 => name = 'benjo',
1 => and
2 => array(
    0 => surname = 'benny',
    1 => or,
    2 => surname = 'bennie',
    ),
3 => age = 4
);

I’ve thought about using recursive functions, and my regular expression for now is :

"#\((([^()]+|(?R))*)\)|(ou[^()])|(et[^()])#",

which of course does not work.

I’ll be glad if someone could help, I’m getting kinda stuck here ! 🙂
Tks,
Romain

LET’S CHANGE THE CHALLENGE ! 🙂
OK, now let’s make it a bit more simple. What would it take with a regular expression and adding the constraint that we stay on “level one” !! No nested parenthesis, just one level, but still as many AND/ORs… Would that change anything in favor or REGEXPs ? (I really would like to avoid writing my mini parser although that sounds really interesting…

  • 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-04T12:11:12+00:00Added an answer on June 4, 2026 at 12:11 pm

    Theoretical regular expression is not powerful enough to do parentheses matching. Theoretical regular expression can only take care of left recursion/right recursion rules. Middle recursion rules is cannot be expressed with regular expression (e.g. <exp> -> "(" <exp> ")").

    Regex in programming languages, however, implements features which allow regex to exceed the power of regular grammar. For example, backreference in regex allows one to write a regex which matches a non context-free languages. However, even with backreference, it’s still not possible to balance parentheses with regex.

    As PCRE library supports recursive regex via subroutine call feature, it is technically possible to parse such an expression with regex. However, unless you can write the regex yourself, which means that you understand what you are doing and can modify the regex to suit your needs, you should just write your own parser. Otherwise, you will end up with an unmaintainable mess.

    (?(DEFINE)
      (?<string>'[^']++')
      (?<int>\b\d+\b)
      (?<sp>\s*)
      (?<key>\b\w+\b)
      (?<value>(?&string)|(?&int))
      (?<exp>(?&key) (?&sp) = (?&sp) (?&value))
      (?<logic>\b (?:and|or) \b)
      (?<main>
        (?<token> \( (?&sp) (?&main) (?&sp) \) | (?&exp) )
        (?:
          (?&sp) (?&logic) (?&sp)
          (?&token) 
        )*
      )
    )
    (?:
      ^ (?&sp) (?= (?&main) (?&sp) $ )
      |
      (?!^) \G
      (?&sp) (?&logic) (?&sp)
    )
    (?:
      \( (?&sp) (?<m_main>(?&main)) (?&sp) \)
      |
      (?<m_key>(?&key)) (?&sp) = (?&sp) (?<m_value>(?&value))
    )
    

    Demo on regex101

    The regex above should be use with preg_match_all, and placed between delimiter with x flag (free spacing mode): /.../x.

    For each match:

    • If m_main capturing group has content, put the content through another round of matching.
    • Otherwise, get the key and value in m_key and m_value capturing group.

    Explanation

    The (?(DEFINE)...) block allows you to define named capturing groups for use in subroutine calls separately from the main pattern.

    (?(DEFINE)
      (?<string>'[^']++')  # String literal
      (?<int>\b\d+\b)      # Integer
      (?<sp>\s*)           # Whitespaces between tokens
      (?<key>\b\w+\b)      # Field name
      (?<value>(?&string)|(?&int)) # Field value
      (?<exp>(?&key) (?&sp) = (?&sp) (?&value)) # Simple expression
      (?<logic>\b (?:and|or) \b) # Logical operators
      (?<main>             # <token> ( <logic> <token> )*
        # A token can contain a simple expression, or open a parentheses (...)
        # When we open a parentheses, we recurse into the main pattern again
        (?<token> \( (?&sp) (?&main) (?&sp) \) | (?&exp) )
        (?:
          (?&sp) (?&logic) (?&sp)
          (?&token) 
        )*
      )
    )
    

    The rest of the pattern is based on this technique to match all <token>s in <token> ( <logic> <token> )* with global matching operation.

    The last part of the regex, while can be written as (?&token), is expanded to match the field name and value in the simple expressions.

    (?:
      \( (?&sp) (?<m_main>(?&main)) (?&sp) \)
      |
      (?<m_key>(?&key)) (?&sp) = (?&sp) (?<m_value>(?&value))
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.