以下是 Java 代码示例,使用正则表达式从文本字符串 'https://movie.douban.com/celebrity/1276086/' 中提取数字:

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

public class Main {
    public static void main(String[] args) {
        String text = 'https://movie.douban.com/celebrity/1276086/';
        Pattern pattern = Pattern.compile('\d+');
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

输出:1276086

解释:

  • 第一行定义了要匹配的文本。
  • 第二行定义了正则表达式,'\d+' 表示匹配连续的数字。
  • 第三行创建了一个 Matcher 对象,并把正则表达式应用到文本中。
  • 第四行使用 while 循环遍历所有匹配的结果,并输出每个匹配的数字。
Java 正则表达式提取文本中的数字 - 示例代码

原文地址: https://www.cveoy.top/t/topic/nekn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录