如何使正则表达式只接受大写和小写字母在Java的口音? [英] How to make regex accept only uppercase and lowercase letters with accents in java?
本文介绍了如何使正则表达式只接受大写和小写字母在Java的口音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只需要大写和小写字母与口音,但code我用失败。
这是我的code:
模式模式= Pattern.compile([A-ZA-Z]);
匹配器匹配= pattern.matcher(testTest);
如果(matcher.find()){
tv.setText(真);
}
其他{
tv.setText(假);
}
解决方案
在我看来,你得到真
,因为正则表达式的重点是寻找字母。它会说假
只,当你有没有字母,在所有测试的字符串。
请考虑改变的if else
语句和正则表达式,以找出是否有其他符号除信件:
模式模式= Pattern.compile([^ W \]);
匹配器匹配= pattern.matcher(testTest);
如果(matcher.find()){
tv.setText(假);
}
其他{
tv.setText(真);
}
希望它帮助。干杯。
I need only uppercase and lowercase letters with accents, but the code I use fails.
This is my code:
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher("testTest");
if (matcher.find()){
tv.setText("true");
}
else{
tv.setText("false");
}
解决方案
In my opinion You get true
, because regex is focused on finding letters. It would say false
only, when You test string with no letters at all.
Please consider changing if else
statement and regex to find out if there are other symbols than letters:
Pattern pattern = Pattern.compile("[^\w]");
Matcher matcher = pattern.matcher("testTest");
if (matcher.find()){
tv.setText("false");
}
else{
tv.setText("true");
}
Hope it helps. Cheers.
这篇关于如何使正则表达式只接受大写和小写字母在Java的口音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文