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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:29:55+00:00 2026-05-25T15:29:55+00:00

It is a well known fact that modern regular expression implementations (most notably PCRE)

  • 0

It is a well known fact that modern regular expression implementations (most notably PCRE) have little in common with the original notion of regular grammars. For example you can parse the classical example of a context-free grammar {anbn; n>0} (e.g. aaabbb) using this regex (demo):

~^(a(?1)?b)$~

My question is: How far can you go? Is it also possible to parse the context-sensitive grammar {anbncn;n>0} (e.g. aaabbbccc) using PCRE?

  • 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-25T15:29:55+00:00Added an answer on May 25, 2026 at 3:29 pm

    Inspired by NullUserExceptions answer (which he already deleted as it failed for one case) I think I have found a solution myself:

    $regex = '~^
        (?=(a(?-1)?b)c)
         a+(b(?-1)?c)
    $~x';
    
    var_dump(preg_match($regex, 'aabbcc'));    // 1
    var_dump(preg_match($regex, 'aaabbbccc')); // 1
    var_dump(preg_match($regex, 'aaabbbcc'));  // 0
    var_dump(preg_match($regex, 'aaaccc'));    // 0
    var_dump(preg_match($regex, 'aabcc'));     // 0
    var_dump(preg_match($regex, 'abbcc'));     // 0
    

    Try it yourself: http://codepad.viper-7.com/1erq9v


    Explanation

    If you consider the regex without the positive lookahead assertion (the (?=...) part), you have this:

    ~^a+(b(?-1)?c)$~
    

    This does nothing more than check that there’s an arbitrary number of as, followed by an equal number of bs and cs.

    This doesn’t yet satisfy our grammar, because the number of as must be the same, too. We can ensure that by checking that the number of as equals the number of bs. And this is what the expression in the lookahead assertion does: (a(?-1)?b)c. The c is necessary so we don’t only match a part of the bs.


    Conclusion

    I think this impressively shows that modern regex is not only capable of parsing non-regular grammars, but can even parse non-context-free grammars. Hopefully this will lay to rest the endless parroting of “you can’t do X with regex because X isn’t regular”

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

Sidebar

Related Questions

Its a well known fact that a static method can work only on static
It's a well known fact, that Oracle treats empty strings as null. However, I'm
It's well known fact that static variable cannot be used in Web Application as
It's well known fact that hibernate work with indexes in a very strange way.
It is a well known fact that structural types are implemented through reflection. Are
Good afternoon, It is a well known fact that when dealing with large files
It is a well known fact that IT projects fail with an alarming rate
It's a relatively well-known fact that Googles' OpenID Provider does not provide (no pun)
It'a a well-known fact that UML does not Turing complete (in contrast to usual
As well-known, C++ has steeper learning curve than most of the mainstream languages, which

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.