0

I understand this may be a duplicate but I have not found an answer that satisfies this question. I have a large string that is set up like so:

season
content content content content content
season
content content content content content
season
content content content content content

etc.

I want to get all of the content in between the "season" string and put that content in to a List. This is my code so far but it does not work, it does not match anything...

String pattern = "season";
    Pattern pattern2 = Pattern.compile(pattern+"(.*?)"+pattern);
    Matcher m = pattern2.matcher(str);
    while(m.find()) {
        System.out.println(m.group());

When I use StringUtils.substringBetween() it does work but I need to get every string in between two "season" strings.

1
  • 1
    Are those new lines? Commented Aug 31, 2014 at 23:46

2 Answers 2

1

Best bet is to use a lookahead to assert either season or the end of the string follows. As well if newline sequences are between the pattern you want to use the dotall flag making the dot . match newlines also.

String s  = "season\nfoobar foobaz\n\nseason\nbarbaz\nseason\nbazquz";
Pattern p = Pattern.compile("(?s)season\\s*(.*?)(?=\\s*season|$)");
Matcher m = p.matcher(s);
List<String> arrayList = new ArrayList<String>();
while (m.find()) {
    arrayList.add(m.group(1));
}
System.out.println(arrayList); // [foobar foobaz, barbaz, bazquz]
Sign up to request clarification or add additional context in comments.

Comments

0

This should work too -

import java.util.ArrayList;
import java.util.List;

public class Season {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String largeString = "season content content content content content season content content content content content season content content content content content";
        List<ArrayList<String>> lists = getStringBetweenSeason(largeString);
        for (ArrayList<String> list : lists) {
            for (String str : list)
                System.out.println(list);
        }

    }

    public static List<ArrayList<String>> getStringBetweenSeason(
            String largeString) {
        if (largeString == null)
            return null;
        List<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();
        for (String str : largeString.split("season")) {
            if (!str.trim().isEmpty()) {
                ArrayList<String> list = new ArrayList<String>();
                for (String strContent : str.split(" ")) {
                    list.add(strContent);
                }
                lists.add(list);
            }
        }
        return lists;
    }
}

----- If pattern matching is the target - then this seem to work too..

public static ArrayList<String> getStringBetweenSeason(String largeString) {
        if (largeString == null)
            return null;
        ArrayList<String> lists = new ArrayList<String>();
        Pattern p = Pattern.compile("season" + "*.*" + "season");
        Matcher m = p.matcher(largeString);

        while (m.find()) {
            lists.add(m.group());
        }

        return lists;
    }

1 Comment

season*.*season Matches seaso some text note the incomplete first wordseason...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.