Want to match the character * in
String s ="foobar*";
I want to get the exact character * with one regex character and I don't want to use exclude everything else like
s.matches("[^\\w]");
to match the asterisk you need this regex:
Pattern asteriskPattern = Pattern.compile("\\*");
But I don't see a good use of that pattern. If all you want to do is check if your string contains it, use
boolean stringContainsAsterisk = string.contains("*");
or find it's index
int indexOfAsterisk = string.indexOf("*");
now the value is -1 if it does not contain the asterisk, or the value of it's index in the string.
关于以下内容:
s.matches("\\*");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.