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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:40:58+00:00 2026-06-06T14:40:58+00:00

This question has been bugging me for a long time now but essentially I’m

  • 0

This question has been bugging me for a long time now but essentially I’m looking for the most efficient way to grab all Strings between two Strings.

The way I have been doing it for many months now is through using a bunch of temporary indices, strings, substrings, and it’s really messy. (Why does Java not have a native method such as String substring(String start, String end)?

Say I have a String:

abcabc [pattern1]foo[pattern2] abcdefg [pattern1]bar[pattern2] morestuff

The end goal would be to output foo and bar. (And later to be added into a JList)

I’ve been trying to incorporate regex in .split() but haven’t been successful. I’ve tried syntax using *‘s and .‘s but I don’t think it’s quite what my intention is especially since .split() only takes one argument to split against.

Otherwise I think another way is to use the Pattern and Matcher classes? But I’m really fuzzy on the appropriate procedure.

  • 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-06T14:40:59+00:00Added an answer on June 6, 2026 at 2:40 pm

    You can construct the regex to do this for you:

    // pattern1 and pattern2 are String objects
    String regexString = Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2);
    

    This will treat the pattern1 and pattern2 as literal text, and the text in between the patterns is captured in the first capturing group. You can remove Pattern.quote() if you want to use regex, but I don’t guarantee anything if you do that.

    You can add some customization of how the match should occurs by adding flags to the regexString.

    • If you want Unicode-aware case-insensitive matching, then add (?iu) at the beginning of regexString, or supply Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE flag to Pattern.compile method.
    • If you want to capture the content even if the two delimiting strings appear across lines, then add (?s) before (.*?), i.e. "(?s)(.*?)", or supply Pattern.DOTALL flag to Pattern.compile method.

    Then compile the regex, obtain a Matcher object, iterate through the matches and save them into a List (or any Collection, it’s up to you).

    Pattern pattern = Pattern.compile(regexString);
    // text contains the full text that you want to extract data
    Matcher matcher = pattern.matcher(text);
    
    while (matcher.find()) {
      String textInBetween = matcher.group(1); // Since (.*?) is capturing group 1
      // You can insert match into a List/Collection here
    }
    

    Testing code:

    String pattern1 = "hgb";
    String pattern2 = "|";
    String text = "sdfjsdkhfkjsdf hgb sdjfkhsdkfsdf |sdfjksdhfjksd sdf sdkjfhsdkf | sdkjfh hgb sdkjfdshfks|";
    
    Pattern p = Pattern.compile(Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2));
    Matcher m = p.matcher(text);
    while (m.find()) {
      System.out.println(m.group(1));
    }
    

    Do note that if you search for the text between foo and bar in this input foo text foo text bar text bar with the method above, you will get one match, which is  text foo text .

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

Sidebar

Related Questions

This question has been puzzling me for a long time now. I come from
This question has been bugging me for some time. I always picture launching my
This question has been bugging me for some time. I've already developed a couple
It's not a programming questions. But this question has been bugging me for awhile.
This question has been bugging me for a while. I am looking for a
Have a question regarding something which has been bugging me for some time now.I'm
I've got a question that has been bugging me for a while now, but
This is a question that's been bugging me for some time now: In photoshop/GIMP,
This question has been bugging me for a while now. When writing a CSS
Sorry for asking such a general question but this has been bugging me for

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.