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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:49:33+00:00 2026-05-24T16:49:33+00:00

I would like to extract sub-string between certain two words using java. For example:

  • 0

I would like to extract sub-string between certain two words using java.

For example:

This is an important example about regex for my work.

I would like to extract everything between “an” and “for“.

What I did so far is:

String sentence = "This is an important example about regex for my work and for me";
Pattern pattern = Pattern.compile("(?<=an).*.(?=for)");
Matcher matcher = pattern.matcher(sentence);

boolean found = false;
while (matcher.find()) {
    System.out.println("I found the text: " + matcher.group().toString());
    found = true;
}
if (!found) {
    System.out.println("I didn't found the text");
}

It works well.

But I want to do two additional things

  1. If the sentence is: This is an important example about regex for my work and for me.
    I want to extract till the first “for” i.e. important example about regex

  2. Some times I want to limit the number of words between the pattern to 3 words i.e. important example about

Any ideas please?

  • 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-24T16:49:34+00:00Added an answer on May 24, 2026 at 4:49 pm

    For your first question, make it lazy. You can put a question mark after the quantifier and then the quantifier will match as less as possible.

    (?<=an).*?(?=for)
    

    I have no idea what the additional . at the end is good for in .*. its unnecessary.

    For your second question you have to define what a “word” is. I would say here probably just a sequence of non whitespace followed by a whitespace. Something like this

    \S+\s
    

    and repeat this 3 times like this

    (?<=an)\s(\S+\s){3}(?=for)
    

    To ensure that the pattern mathces on whole words use word boundaries

    (?<=\ban\b)\s(\S+\s){1,5}(?=\bfor\b)
    

    See it online here on Regexr

    {3} will match exactly 3 for a minimum of 1 and a max of 3 do this {1,3}

    Alternative:

    As dma_k correctly stated in your case here its not necessary to use look behind and look ahead. See here the Matcher documentation about groups

    You can use capturing groups instead. Just put the part you want to extract in brackets and it will be put into a capturing group.

    \ban\b(.*?)\bfor\b
    

    See it online here on Regexr

    You can than access this group like this

    System.out.println("I found the text: " + matcher.group(1).toString());
                                                            ^
    

    You have only one pair of brackets, so its simple, just put a 1 into matcher.group(1) to access the first capturing group.

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

Sidebar

Related Questions

I would like to extract the date a jpg file was created. Java has
I have the following snippet where I would like to extract code between the
I have a NSString like this: 456673\tSomething But I would like to extract Something
Better way of striping a string I would like to extract the number from
I would like to extract the first part of this hostname testsrv1 from testsrv1.main.corp.loc.domain.com
I would like to extract incoming mail's attachment using bash or python or any
I would like to extract from a general HTML page, all the text (displayed
I have a full MS SQL Backup file that I would like to extract
I would like to know if it is possible, To extract the userid from
In Nant, I would like to be able to extract the last name of

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.