简体   繁体   English

如何控制Java正则表达式中的迭代步骤?

[英]How to control the iteration step in Java regex?

The topic is confusing, however for example, 然而,这个话题令人困惑,例如,

final String pattern = "(abc)";

final String content = "dabcef";

Matcher m = Pattern.compile(pattern).matcher(content);

The m.find() will surely return true. m.find()肯定会返回true。

I want to know if it's possible to process chars only once, meaning 我想知道是否有可能只处理一次字符,这意味着

"dab" -> not found, "cef" -> not found, over. “dab” - >未找到,“cef” - >未找到,结束。

Thanks! 谢谢!

EDIT: 编辑:

Actually I want to find all matches instead of only check if matches or not. 其实我想找到所有的比赛,而不是只检查是否匹配。 For example, 例如,

abc abc def abc dab cef (actually without spaces) abc abc def abc dab cef(实际上没有空格)

will be matched by ^(.{3})*?(abc), however only once. 将匹配^(。{3})*?(abc),但只有一次。 And I expect 3 matches. 我期待3场比赛。

Thanks! 谢谢!

怎么样:

final String pattern = "^(.{3})*(abc)";

I found the solution by moving start index. 我通过移动起始索引找到了解决方案。 Thank @Oil for the suggestion! 感谢@Oil的建议!

public static void main(String[] args) {
    final String pattern1 = "^(.{3})*?(abc)";

    final String content1 = "efabcabcdabcefaabcdfabce"; // two matches

    final String content2 = "abcabcdabcefabc"; // three matches

    Matcher mStart = Pattern.compile(pattern1).matcher(content1);

    while (mStart.find()) {
        System.out.println(mStart.group(mStart.groupCount()));
        System.out.println(mStart.start() + ", " + mStart.end());

        mStart = mStart.region(mStart.end(), mStart.regionEnd());
    }

    //-----------------------------
    System.out.println("------------------------");

    mStart = Pattern.compile(pattern1).matcher(content2);

    while (mStart.find()) {
        System.out.println(mStart.group(mStart.groupCount()));
        System.out.println(mStart.start() + ", " + mStart.end());

        mStart = mStart.region(mStart.end(), mStart.regionEnd());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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