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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:36:42+00:00 2026-05-20T00:36:42+00:00

I am trying to write a regular expression to match ruby’s list and hash

  • 0

I am trying to write a regular expression to match ruby’s list and hash syntaxes, e.g.:

[:a, "b", c, 3]

{:a => [
    1,2,3
]}

[1, {
    a => "t", :b => "w",
    :c => :o
}, 3]

The issue, of course, is with the nested/recursive nature of the things. I have a sneaking suspicion that such nested structures cannot actually be expressed as a regular expression as that ‘language’ is not regular. I expect the solution would have to involve subroutines and recursion, however I’m struggling to get my head around it. Can anyone confirm/deny my suspicions or offer a solution?

Any help appreciated.

Edit: as a note, I’m using PHP’s preg_* methods mainly

Edit: as another note, I’ve created a routine, <ruby_value> to match keys and scalar values.

Edit: I should specify that I’m more interested in this “out of interest”. I have already wrote a mini-parser for these things in PHP however I am interested to see if a not-necessarily-pure-regex solution exists.

E.g. equal nested brackets:

/^(?<paren_expr>
    \( (?: (?&paren_expr) | ) \)
)$/x

Which is a valid PHP regex and will match “(())”, “()” and “((((((()))))))” but not “(” or “(()” etc.

  • 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-20T00:36:43+00:00Added an answer on May 20, 2026 at 12:36 am

    You are correct that nested structures are not a regular language, and thus cannot be expressed via pure regular expressions.

    PCRE has the ability to specify recursive regexes, although I’m not sure whether PHP’s implementation of them includes that support.

    Really though, what you want to do is write a state machine yourself (with memory of nesting).

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

Sidebar

Related Questions

I am trying to write a regular expression string match in vb.net. The condition
I am trying to write a regular expression to match and split a custom
I am trying to write a regular expression in Javascript to match a name
I'm trying to write a python regular expression that will match both these URLs:
I'm trying to write a regular expression for Java's String.matches(regex) method to match a
In java, I'm trying to write a regular expression that will match a unit
I'm trying to write a regular expression that validates a date. The regex needs
I am trying to write a regular expression to strip all HTML with the
I'm trying to write a regular expression that surrounds http URLs with angle brackets,
I am trying to write a replacement regular expression to surround all words in

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.