java 正则表达式 至少含有一个数字
时间: 2024-01-30 19:11:13 浏览: 159
以下是使用Java正则表达式判断字符串是否至少含有一个数字的示例代码:
```java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String regex = ".*\\d.*"; // 包含至少一个数字的正则表达式
String input1 = "abc123";
String input2 = "abcdef";
Pattern pattern = Pattern.compile(regex);
Matcher matcher1 = pattern.matcher(input1);
boolean containsNumber1 = matcher1.matches();
System.out.println("Input1 contains at least one number: " + containsNumber1); // 输出:true
Matcher matcher2 = pattern.matcher(input2);
boolean containsNumber2 = matcher2.matches();
System.out.println("Input2 contains at least one number: " + containsNumber2); // 输出:false
}
}
```
阅读全文