gpt4 book ai didi

java - Java中使用模式匹配查找不包含数字的字符串数据

转载 作者:行者123 更新时间:2023-12-02 08:59:53 25 4
gpt4 key购买 nike

期待matchString 带有 no数字,使用 regex 中的 \D

每个字符串都显示为false,但为什么呢?

输出:

thufir@dur:~/NetBeansProjects/parseCSV$ 
thufir@dur:~/NetBeansProjects/parseCSV$ gradle run

> Task :run
Feb. 16, 2020 5:24:49 A.M. parseCSV.FileHelper processLines
INFO: false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: z10 false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: y9 false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: x7 false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: atrib6 false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: alice false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: home5 false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: cell4 false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: sue false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: phone3 false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: phone2 false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: phone1 false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: joe false
Feb. 16, 2020 5:24:50 A.M. parseCSV.FileHelper processLines
INFO: people false

BUILD SUCCESSFUL in 1s
3 actionable tasks: 1 executed, 2 up-to-date
thufir@dur:~/NetBeansProjects/parseCSV$

相关方法from类:

public void processLines() {
String regex = "\\D";
boolean isDigit = false;
for (String s : lines) {
isDigit = Pattern.matches(regex, s);
log.info(s + "\t\t" + Boolean.toString(isDigit)
);

}

最佳答案

您应该使用正则表达式模式 ^\D+$,它从头到尾匹配一个或多个个连续的非数字字符。由于 Pattern#matches 隐式覆盖整个字符串,因此我们可以使用 \D+:

public void processLines() {
String regex = "\\D+";
boolean isDigit = false;
for (String s : lines) {
isDigit = Pattern.matches(regex, s);
log.info(s + "\t\t" + Boolean.toString(isDigit)
);
}
}

关于java - Java中使用模式匹配查找不包含数字的字符串数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60248988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com