正则表达式仅在使用换行符分隔时匹配多个子字符串

时间:2017-08-06 22:00:19

标签: java android regex

这是我现在的设置:

private static Pattern bbCodePattern;
private static Matcher patternMatcher;

private static final String BB_GENERIC = "\\[(.*)\\](.*?)\\[\\/(.*)\\]";

...

public static List<PostComponent> process(final String bbCode) {

    List<PostComponent> postComponents = new ArrayList<>();

    patternMatcher = bbCodePattern.matcher(bbCode);

    while(patternMatcher.find()) {
        postComponents.add(new PostComponent(patternMatcher.group(2), getComponentType(patternMatcher.group(1))));
    }

    return postComponents;

}

...

bbCode[img]https://forum.com/image.png[/img] [spoiler]texttohide[/spoiler]时,返回的列表只有1个值,而如果是[img]https://forum.com/image.png[/img]\n[spoiler]texttohide[/spoiler],则返回的列表具有预期的2个元素。是什么导致了这种行为?

0 个答案:

没有答案