在java中的模式匹配中重用消耗的字符?

发布于 2024-12-28 13:11:31 字数 312 浏览 1 评论 0原文

考虑以下模式:-

aba

接下来。源字符串:-

abababbbaba

01234567890    //Index Positions

使用 java.util.regex 包中的 Pattern 和 Matcher 类,仅查找此模式两次,因为 regex 不考虑已使用的字符。

如果我想重复使用部分已消耗的字符怎么办?也就是说,我想要在这里匹配 3 个匹配项,一个在位置 0,一个在位置 2(之前被忽略),一个在位置 8。

我该怎么做?

Consider the following Pattern :-

aba

And the foll. source string :-

abababbbaba

01234567890    //Index Positions

Using Pattern and Matcher classes from java.util.regex package, finds this pattern only two times since regex does not consider already consumed characters.

What if I want to reuse a part of already consumed characters. That is, I want 3 matches here, one at position 0, one at 2 (which is ignored previously), and one at 8.

How do I do it??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左耳近心 2025-01-04 13:11:31

我认为你可以使用 indexOf() 类似的东西。

String str = "abababbbaba";
        String substr = "aba";
        int location = 0;
        while ((location = str.indexOf(substr, location)) >= 0)
        {
            System.out.println(location);
            location++;
        }

打印:

0、2 和 8

I think you can use the indexOf() for something like that.

String str = "abababbbaba";
        String substr = "aba";
        int location = 0;
        while ((location = str.indexOf(substr, location)) >= 0)
        {
            System.out.println(location);
            location++;
        }

Prints:

0, 2 and 8

百变从容 2025-01-04 13:11:31

您可以使用前瞻来实现这一点。现在您拥有的是 group(1) 中的第一个位置和 group(2) 中的第二个匹配项。两者都使您正在搜索的句子中的每个字符串的长度为 3。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Question8968432 {
    public static void main(String args[]) {
        final String needle = "aba";
        final String sentence = "abababbbaba";
        final Matcher m = Pattern.compile("(.)(?=(..))").matcher(sentence);
        while (m.find()) {
            final String match = m.group(1) + m.group(2);
            final String hint = String.format("%s[%s]%s",
                sentence.substring(0, m.start()), match, 
                sentence.substring(m.start() + match.length()));
            if (match.equals(needle)) {
                System.out.printf("Found %s starting at %d: %s\n", 
                    match, m.start(), hint);
            }
        }
    }
}

输出:

Found aba starting at 0: [aba]babbbaba
Found aba starting at 2: ab[aba]bbbaba
Found aba starting at 8: abababbb[aba]

您可以跳过最终字符串提示部分,这只是为了向您展示它匹配的内容和位置。

You can use a look ahead for that. Now what you have is the first position in group(1) and the second match in group(2). Both making each String of length 3 in the sentence you are searching in.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Question8968432 {
    public static void main(String args[]) {
        final String needle = "aba";
        final String sentence = "abababbbaba";
        final Matcher m = Pattern.compile("(.)(?=(..))").matcher(sentence);
        while (m.find()) {
            final String match = m.group(1) + m.group(2);
            final String hint = String.format("%s[%s]%s",
                sentence.substring(0, m.start()), match, 
                sentence.substring(m.start() + match.length()));
            if (match.equals(needle)) {
                System.out.printf("Found %s starting at %d: %s\n", 
                    match, m.start(), hint);
            }
        }
    }
}

Output:

Found aba starting at 0: [aba]babbbaba
Found aba starting at 2: ab[aba]bbbaba
Found aba starting at 8: abababbb[aba]

You can skip the final String hint part, this is just to show you what it matches and where.

回首观望 2025-01-04 13:11:31

如果您可以更改正则表达式,那么您可以简单地使用如下内容:

a(?=ba)

If you can change the regexp, then you can simply use something like:

a(?=ba)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文