Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I have a String:
String filename = "somefile%dh.mm a%d.txt";
to be sure that filename contains two %d, I use this regex:
%d
System.out.println((filename.matches("%d.*%d")));
but it returns false. What's wrong?
You need to match the whole word, including characters before and after the two %d:
filename.matches(".*%d.*%d.*")
它返回 False,因为matches()将完整的输入字符串与模式进行比较。
matches()
要检查您的字符串是否包含模式,请使用该find()方法。
find()
System.out.println((filename.find("%d.*%d")));
如果模式出现在字符串中的某处,这将返回 true。