简体   繁体   中英

Find all strings in between two strings multiple times

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.

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]

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM