本文介绍了用于java的String.matches方法的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上我的问题是,为什么:
Basically my question is this, why is:
String word = "unauthenticated";
word.matches("[a-z]");
返回false? (在java1.6中开发)
returning false? (Developed in java1.6)
基本上我想看看传递给我的字符串是否包含alpha字符。
Basically I want to see if a string passed to me has alpha chars in it.
推荐答案
String.matches()
函数将正则表达式与整个字符串匹配(好像你的正则表达式在开始时有 ^
,最后是 $
。如果要在字符串中的某处搜索正则表达式,请使用。
The String.matches()
function matches your regular expression against the whole string (as if your regex had ^
at the start and $
at the end). If you want to search for a regular expression somewhere within a string, use Matcher.find()
.
正确的方法取决于关于你想做什么:
The correct method depends on what you want to do:
- 检查你的输入字符串是否包含完全的字母字符(
String.matches()
[az] +
) - 查看您的输入字符串是否包含任何字母字符(可能还有其他字符)(
Matcher.find()
[az]
)
- Check to see whether your input string consists entirely of alphabetic characters (
String.matches()
with[a-z]+
) - Check to see whether your input string contains any alphabetic character (and perhaps some others) (
Matcher.find()
with[a-z]
)
这篇关于用于java的String.matches方法的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!