CoreJava/test/RegexTest.java
2024-05-15 23:50:53 +08:00

17 lines
505 B
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package test;
public class RegexTest {
public static void main(String[] args) {
String regex = "^[a-zA-Z][a-zA-Z1-9]*$";
String testString = "Abc123"; // 测试字符串
boolean matches = testString.matches(regex);
if (matches) {
System.out.println("字符串仅由字母和数字1-9组成");
} else {
System.out.println("字符串包含非字母或非数字1-9的字符");
}
}
}