我正在尝试测试我的字符串是否包含字符 (a-z) (A-Z) 和数字 (0-9)
package testing;
import java.io.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Testing extends JPanel
{
public static void main(String[] args)
{
String foo = "abc1";
if (foo.matches(".*[0-9].*") && foo.matches(".*[A-Z].*") && foo.matches(".*[a-z].*"))
{
//contain letters and numbers only
System.out.println("Foo");
}
}
}
我期望打印出Foo,但我的正则表达式似乎有问题。有人可以帮我吗 ??
谢谢
最佳答案
改变
foo.matches(".*[0-9].*") && foo.matches(".*[A-Z].*") && foo.matches(".*[a-z].*")
至
foo.matches(".*[0-9].*") && (foo.matches(".*[A-Z].*") || foo.matches(".*[a-z].*"))
关于java - 用于检查字母数字字符串的正则表达式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23508319/