0

I have a String:

String filename = "somefile%dh.mm a%d.txt";

to be sure that filename contains two %d, I use this regex:

System.out.println((filename.matches("%d.*%d")));

but it returns false. What's wrong?

4

2 回答 2

6

You need to match the whole word, including characters before and after the two %d:

filename.matches(".*%d.*%d.*")
于 2013-05-27T09:13:35.590 回答
1

它返回 False,因为matches()将完整的输入字符串与模式进行比较。

要检查您的字符串是否包含模式,请使用该find()方法。

System.out.println((filename.find("%d.*%d")));

如果模式出现在字符串中的某处,这将返回 true。

于 2013-05-27T09:33:59.237 回答