Java正则表达式:检查单词是否包含非字母数字字符 [英] Java regex: check if word has non alphanumeric characters
本文介绍了Java正则表达式:检查单词是否包含非字母数字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,用于确定单词是否包含任何非字母数字字符:
This is my code to determine if a word contains any non-alphanumeric characters:
String term = "Hello-World";
boolean found = false;
Pattern p = Pattern.Compile("\\W*");
Matcher m = p.Matcher(term);
if(matcher.find())
found = true;
我想知道正则表达式是否错误。我知道\ W
会匹配任何非单词字符。关于我缺少什么的任何想法??
I am wondering if the regex expression is wrong. I know "\W"
would matches any non-word characters. Any idea on what I am missing ??
推荐答案
将你的正则表达式改为:
Change your regex to:
.*\\W+.*
这篇关于Java正则表达式:检查单词是否包含非字母数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文