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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:20:44+00:00 2026-05-31T13:20:44+00:00

I have a fairly simple regex problem for a little personal experiment that I

  • 0

I have a fairly simple regex problem for a little personal experiment that I haven’t quite figured out.

In a string, I might have several <tag>[some characters here] that I need to match. The obvious way to do it would be with a /<tag>\[.*?\]/ regex, to match any characters after the <tag>[ and before the ].

I’d like to be able to have <tag>s within <tag>s, however. This causes a problem. If I had the following:

<tag>[some characters <tag>[in here] to match]

the regex would stop matching as soon as it reached the first closing-bracket, and completely fail to match the last part of the statement. I’ve tried to solve the problem by telling the regex to ignore any internal <tag>s, so I can do a match on the stripped contents later. I haven’t quite gotten it working. The closest I’ve come is:

/<tag>\[(.*?(?:<tag>\[.*?\])*?.*?)\]/

which doesn’t quite work. I would hope that it would match any number of characters, and any inner tags if they exist. It still has trouble with that first closing bracket, however.

Maybe somebody who’s better at regular expressions knows a good solution to this.

  • 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-31T13:20:45+00:00Added an answer on May 31, 2026 at 1:20 pm

    Though you should probably drop regex and do this manually if the mini-language becomes more complex, you can use recursive regex.

    Your regex would look something like this:

    /(?<reg>(\w+\[([^\]\[]|\g<reg>)*\]))/
    

    You can see it in action here: http://rubular.com/r/9F7isgZpj9

    Here is the regex broken down to its parts:

    (?<reg>(    # start a regex named "reg"
      \w+       # the tag name
      \[        # open bracket
      (         # which can contain
        [^\]\[] # non-bracket characters
        |       # or
        \g<reg> # sub-tags (this is where the magic happens)
      )*        # zero or more times
      \]        # close the tag
     )
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple problem. I'm trying to make a function that accepts
I have a fairly simple const struct in some C code that simply holds
I have a fairly simple sync problem. I have a table with about 10
I have a fairly simple python loop that calls a few functions, and writes
I have fairly simple problem but I can not think of the simple solution.
I have a fairly simple database that I inherited. For the purposes of this
I have a fairly simple console app that monitors an exchange mailbox, picks particular
I have a fairly simple query that runs super fast most of the time
I have a fairly simple Windows Forms application that I would like to add
I have a fairly simple if else statement in C# that looks something like

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.