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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:37:54+00:00 2026-06-13T23:37:54+00:00

My question is fairly straightforward, even if the purpose it will serve is pretty

  • 0

My question is fairly straightforward, even if the purpose it will serve is pretty complicated. I will use a simple example:

AzzAyyAxxxxByyBzzB

So normally I would want to get everything between A and B. However, because some of the content between the first A and the last B (one pair) contains additional AB pairs I need to push back the end of the match. (Not sure if that last part made sense).

So what I’m looking for is some RegEx that would allow me to have the following output:

Match 1
  Group 1: AzzAyyAxxxxByyBzzB
  Group 2: zzAyyAxxxxByyBzz

Then I would match it again to get:

Match 2
  Group 1: AyyAxxxxByyB
  Group 2: yyAxxxxByy

Then finally again to get:

Match 3
  Group 1: AxxxxB
  Group 2: xxxx

Obviously if I try (A(.*?)B) on the whole input I get:

Match x
  Group 1: AzzAyyAxxxxB
  Group 2: zzAyyAxxxx

Which is not what I’m looking for 🙂

I hope this makes sense. I understand if this can’t be done in RegEx, but I thought I would ask some of you regex wizards before I give up on it and try something else. Thanks!

Additional Info:

The project I’m working on is written in Java.

One other problem is that I’m parsing a document which could contain something like this:

AzzAyyAxxxxByyBzzB
Here is some unrelated stuff
AzzAyyAxxxxByyBzzB
AzzzBxxArrrBAssssB

And the top AB pairs needs to be separate from the bottom AB pairs

  • 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-13T23:37:56+00:00Added an answer on June 13, 2026 at 11:37 pm

    You made your regex explicitly ungreedy by using the ?. Just leave it out and the regex will consume as much as possible before matching the B:

    (A(.*)B)
    

    However, in general nested structures are beyond the scope of regular expressions. In a case like this:

    AxxxByyyAzzzB
    

    You would now also match from the first A to the last B. If this is possible in your scenario, you might be better of going through the string yourself character-by-character and counting As and Bs to figure out which ones belong together.

    EDIT:

    Now that you have updated the question and we figured this out in the comments, you do have the problem of multiple consecutive pairs. In this case, this cannot be done with a regex engine that does not support recursion.

    However you can switch to matching from the inside out.

    A([^AB]*)B
    

    This will only get innermost pairs, because there can be neither an A nor a B between the delimiters. If you find it, you can then remove the pair and continue with your next match.

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

Sidebar

Related Questions

A simple question with a fairly complicated answer: How do you sign a Java
This seems like a fairly straightforward question, but I couldn't find this particular use-case
Fairly straightforward question. In C++ the parent constructor will be implicitly called before the
Fairly straightforward question, I have images stored in my database as varbinary and would
Fairly straightforward question here, but I can't seem to find someone who has asked
This question is fairly fundamental.I gave a simple and straighfoward test on my cygwin:
A fairly simple question for which I have a guess, but I can't find
Fairly simple question: I have an init method on my class that has the
fairly simple question and I haven't seen anything asked with this exact scenario laid
I believe this is a fairly simple question but it is something I am

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.