Java 正则表达式提取字符串中的数字
以下是一个简单的Java代码示例,使用正则表达式判断字符串中的数字:
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
String input = 'common GE2/0/17(D) GE2/0/19(D) GE2/0/20(D) GE2/0/22(D)';
String regex = '\d+'; // 匹配一个或多个数字
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
以上代码将输出:
2
0
17
2
0
19
2
0
20
2
0
22
原文地址: https://www.cveoy.top/t/topic/v2p 著作权归作者所有。请勿转载和采集!