我得到一个包含逗号分隔数据的csv文件。其中一些数据可能包含excel单元格编号,如b1,b2,c1,表示MS excel单元格编号
CSV数据示例
b1,2 3 4 b1,5 c2 3 d2,5 4,2 e1
我需要确定是否有任何csv数据包含a1,c1或b1等数据。
即。我需要查找数据是否包含一个字符后跟一个数字。
我使用JAVA正则表达式编写了以下程序。
虽然这在数据仅包含b1或c1时有效,但是当数据在其之前或之后包含更多字符时,它无法找到b1或c1。
例如
示例1工作并打印True
package com.test;
public class PatternTest {
public static void main(String[] args) {
String pattern = "(([A-Za-z].*[0-9]))";
String data = "b2";
if(data.matches(pattern)){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
示例2不起作用并打印错误。如何使示例2工作,以便它可以在包含更多字符串的字符串中找到b1或c1或a1或a2
package com.test;
public class PatternTest {
public static void main(String[] args) {
String pattern = "(([A-Za-z].*[0-9]))";
String data = "1 b2 3 4 ";
if(data.matches(pattern)){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
答案 0 :(得分:0)
请忽略。我发现解决方案如下所示
package com.test;
public class PatternTest {
public static void main(String[] args) {
String pattern = "((.*[A-Za-z].*[0-9].*))";
String data = "c2 3 c2 *";
if(data.matches(pattern)){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
答案 1 :(得分:0)
你可以这样做:
String str = "b1, 2 3 4 b1, 5 c2 3 d2, 5 4, 2 e1";
if (str.matches(".*\\p{Alpha}\\d.*")) {
System.out.println(true);
} else {
System.out.println(false);
}
// Result is true for current str
在您自己回答的案例中对于 b22,b&amp; 2,cc3等这些你不想要的字符串<{1}}也是true