[英](Problems with java.util.regex.Pattern) checking string for digits and letters
I have a task to check if a new string (login) complies some rules These are rules: 我有一个任务来检查新字符串(登录)是否符合某些规则这些是规则:
1. It has to be 1-20 symbols length
2. It may consist of digits, letters (a-z,A-Z), dots and "-"
3. The first symbol has to be a letter (a-z,A-Z)
4. It has to end with a letter or a digit
So i thought of using java.util.regex.Pattern, and I ran in some problems. 所以我想到了使用java.util.regex.Pattern,我遇到了一些问题。 Thats what i have: 多数民众赞成我所拥有的:
String login = "a.bc-.52";
Pattern p1 = Pattern.compile("([a-zA-Z]{1}[a-zA-Z\\d.-]{0,18}[a-zA-Z\\d])");
Matcher m1 = p1.matcher(login);
boolean b = m1.matches();
I'm having trouble to check if string ends with a letter or a diggit. 我无法检查字符串是以字母还是以diggit结尾。 I think i could split the string and check its parts with different patterns, but something tells me, that it can be done easier. 我想我可以拆分字符串并用不同的图案检查它的部分,但有些东西告诉我,它可以更容易地完成。
I'm sorry if it is a dumb questions or i've made mistakes. 如果这是一个愚蠢的问题或者我犯了错误,我很抱歉。 Perhaps there is easier way, please tell me. 也许有更简单的方法,请告诉我。 Thank you 谢谢
^[a-zA-Z]{1}[a-zA-Z\\d.-]{0,18}[a-zA-Z\\d]$
^
<- ensures that the string begins with a letter ^
< - 确保字符串以字母开头
$
<- ensures that the string ends with a letter of digit $
< - 确保字符串以数字字母结尾
What you are looking for is the dollar sign. 你要找的是美元符号。 Simple as that: 就那么简单:
t$ --> ends in t t $ - >以t结尾
The opposite is ^ 相反的是^
^a - starts with a ^ a - 以a开头
So in your case just add the dollar sign at the end. 因此,在您的情况下,最后添加美元符号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.