immzi
immzi

Reputation: 121

RegEx to match a string between a set of words

I am trying to match a group of words between two words in a String. I will be using Java RegEx.

Input Text

The clever fox JUMPED OVER the big dog and ran away.

Expected Output

the big

RegEx Used

(?<=(fox\s[A-Z0-9]*))(?s)(.*?)(?=\sdog)

I get below output which gives me all words between fox and dog

JUMPED OVER the big

The word "fox" will be followed by one or more all upper case words always. I need to match all the words following these two words till I get "dog".

Also I need to get the desired output in Capture Group 0. I can not use different capture groups. This is a limitation in my application.

Any help on this is greatly appreciated.

Upvotes: 0

Views: 325

Answers (3)

Mike H-R
Mike H-R

Reputation: 7815

I'm afraid java doesn't support variable length look behind assertions.

In addition capture group 0 is the full text which due to variable length lookbehinds not being allowed (as explained before) is impossible unless you know there is always going to be a certain length of uppercase words.

To do this with capture group 1 try:

(?<=fox)(?:\s[A-Z0-9]*)*\s?(.*?)(?=\sdog)

EDIT: Fixed typo in regex

EDIT 2: clarified fulltext problem.

EDIT 3: Depending on how stupid java is with "non-obvious maximum length of lookbehind group" this might work: (?<=fox(?:\s[A-Z0-9]{5,7}){1,2})(.*?)(?=\sdog) but I need to ask, what makes you so sure that you need this to be capture group 0? I somewhat doubt that's the case, even if it does you can just take the output and then run it again against .* to get a regex of capture group 0, there's no way you really need this as a requirement.

Upvotes: 1

Andynedine
Andynedine

Reputation: 441

You can use this regex:

^.*fox[A-Z0-9\s]*(.*)dog.*$

You can pass fox and dog by parameter in your function to use in other cases.

Upvotes: 1

user1907906
user1907906

Reputation:

Without regexp:

    String fox = "The clever fox JUMPED OVER the big dog and ran away.";

    boolean start = false;
    for (String word : fox.split("\\s")) {
        if ("fox".equals(word)) {
            start = true;
            continue;
        }
        if ("dog".equals(word)) {
            break;
        }
        if (start) {
            System.out.println(word);
        }
    }

Upvotes: 0

Related Questions