java正则获取下面文本中的数字 httpsmoviedoubancomcelebrity1276086
以下是Java代码示例:
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循环遍历所有匹配的结果,并输出每个匹配的数字。
原文地址: https://www.cveoy.top/t/topic/bMEx 著作权归作者所有。请勿转载和采集!