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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:45:13+00:00 2026-05-20T20:45:13+00:00

In my own markup language, I have a quotation tags >>which use these characters

  • 0

In my own markup language, I have a quotation tags >>which use these characters to make a blockquote<<. The problem starts when there is a nested blockquote:

>>(1)
start1
  >>(2)quote 2!<<(3)
<<(4)

I would like to match only the most outer tags, like that:

<blockquote>
start1
  >>quote 2!<<
</blockquote>

If I try a simple ungreedy regex />>(.+?)<</, (1) and (3) will be matched and (2) and (4) won’t be ever matched. If I make it ungreedy />>(.+)>>/ (1) and (4) will successfully match (and by recursively calling the function I can then match (2) with (3)), but it won’t work when I will have two blocks in the same piece of text:

>>(A)quote1<<(B)

>>(C)quote2<<(D)

The greedy one will match (A) with (D), leaving (B) and (C) alone. I suppose I’d have to somehow make it “Ungreedy, but only if there are no other pairs inside”, which goes beyond my skills. Is there a way to make it work correctly? So (1) matches (4), (A) matches (B) and (C) matches (D)? If you can think of non regexp solution (but not a parser) then it would be good enough for me too. I am not asking how to also make the (2) match (3), just how to skip them (or any other nested pairs) successfully.

Success! Inspired by Arjen’s suggestion, in the end I used such construction (not necessarily working:

$text = str_replace('([^>]|^)>([^>]|$)', '$1&gt;$2', $text);
while ($len != strlen($text)){
    $len = strlen($text);
    $text = preg_replace_callback('/>>([^>]+?)<</', "blockHashFunction", $text);
}

ie. I first encode all single >’s and then perform a recursive preg_replace. Hashing in this case means that the >>asdsad<< is replaced by, for example "\xFE:3:\xFE" which, at the end of the script it is unhashed (well, more like decoded actually, I guess) into the proper <blockquote>asdsad</blockquote>.

  • 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-20T20:45:14+00:00Added an answer on May 20, 2026 at 8:45 pm

    Regular expressions are not really suited for this kind of parsing. There actually are some RegEx engines that do support for nested/balanced matching, like the .NET Framework RegEx engine (see: http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396452.aspx). However, I feel this leads to very complex patterns.

    You are much better if you create a regular expression that matches a begin or end-tag and manually create a tree of all matches. After processing the entire string you can discard the unwanted matches from the resulting collection.

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

Sidebar

Related Questions

I got my own custom font working in Java, but I have one problem,
I have created my own component and I want to do something similar to
I am writing my own API to perform some of the functionality of the
I'm developing my own PHP extension with a class in it. I recently upgraded
I created a my own SortedSet here is the code for adding something to
How to create our own wifi hotspot. I need some android devices connect to
I was following the Contoso University tutorial on the asp.net website and it all
in a Visual Studio Extension Package I want to create a ToolWindow (like Solution
I want to build an app that renders events into it's UI but I
A bit of an architectural question... I originally created a Javascript singleton to house

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.