简体   繁体   English

(java.util.regex.Pattern的问题)检查字符串的数字和字母

[英](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.

相关问题 java.util.regex.Pattern的正则表达式 - Regular expression for java.util.regex.Pattern Java反射:在JRuby中确定java.util.regex.Pattern的类 - Java Reflection: Determining class of java.util.regex.Pattern in JRuby 使用java.util.regex.Pattern在java中查找类似的IP - find similar IP in java using java.util.regex.Pattern Liferay中java.util.regex.Pattern处的java.lang.StackOverflowError - java.lang.StackOverflowError at at java.util.regex.Pattern in liferay 添加到java.util.regex.Pattern或合并它们 - Adding onto a java.util.regex.Pattern or Merging Them java.util.regex.Pattern 可以进行部分匹配吗? - Can java.util.regex.Pattern do partial matches? 如何使用 protobuf 序列化 java.util.regex.Pattern? - How to serialize a java.util.regex.Pattern using protobuf? java.util.regex.PatternSyntaxException:null(在java.util.regex.Pattern中)错误 - java.util.regex.PatternSyntaxException: null (in java.util.regex.Pattern) error java.util.regex.Pattern和java.util.regex.Matcher的设计有什么好处? - What is benefit in design of java.util.regex.Pattern and java.util.regex.Matcher? java.util.regex.Pattern $ BmpCharProperty.match处的java.lang.StackOverflowError(Pattern.java:3715) - java.lang.StackOverflowError at java.util.regex.Pattern$BmpCharProperty.match(Pattern.java:3715)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM