正则表达式搜索超出字符串边界 [英] Regex searches beyond string boundary

查看:153
本文介绍了正则表达式搜索超出字符串边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

import java.util.regex.*;

public class RegEx {

    public static void main(String[] args) {

        Pattern p = Pattern.compile("\\d*");
        Matcher m = p.matcher("ab56ef");
        System.out.println("Pattern is " + m.pattern());
        while (m.find()) {
            System.out.print("index: " + m.start() + " " + m.group());
        }
    }
}

结果是:

index: 0 index: 1 index: 2 56 index: 4 index: 5 index: 6

由于ab34ef长度为6,因此字符串的最高索引为5.

为什么索引处有匹配6 ?提前谢谢你!

Since "ab34ef" length is 6, the string's highest index is 5.
Why is there a match at index 6? Thank you in advance!

推荐答案

你有6个索引返回,因为这里有6个匹配,因为 \ d * 可以匹配空字符串。在输入字符串中的每个字符之前总是有一个空字符串,因为正则表达式引擎在每个位置处理文本以查找边界或特定字符。

You have 6 indices returned because there are 6 matches here since \d* can match an empty string. There is always an empty string before each character in an input string, because the regex engine is processing text at each position looking for boundaries or specific characters.

这是< a href =https://regex101.com/r/eV4qR2/1 =nofollow noreferrer>可视化:

此处,引擎检查a的开头字符串,并说:我看不到数字,但我可以返回一个匹配,因为数字位数可以是0。它返回空字符串作为匹配,然后继续 b 。依此类推,直到字符串结束。

Here, the engine examines the beginning of a string, and says: "I see no digit, but I can return a match, since the number of digits can be 0". It returns the empty string as a match, and goes on to b. And so on until the end of string.

如果您需要查找所有数字,只需使用 + 量词 \d 简写类。

If you need to find all numbers, just use a + quantifier with \d shorthand class.

参见 IDEONE演示

这篇关于正则表达式搜索超出字符串边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆