[英]Java regular expression to check if there is at least one letter
I've found lot of variations on this subject on both SO and web, but most (if not all) ask for at least one letter and one digit. 我在SO和网络上都发现了很多关于这个主题的变化,但大多数(如果不是全部的话)要求至少一个字母和一个数字。 I need to have at least one letter.
我需要至少一封信。 I've tried but I haven't make it right, what I need is that String contain only letters, letters + numbers (any order), dashes and spaces are allowed but not at the beginning or the end of the string.
我已经尝试但我没有做对,我需要的是String只包含字母,字母+数字(任何顺序),破折号和空格是允许的,但不是在字符串的开头或结尾。 Here is how it looks like right now:
以下是它现在的样子:
protected static final String PATTERN = "[\u00C0-\u017Fa-zA-Z0-9']+([- ][\u00C0-\u017Fa-zA-Z0-9']+)*";
public static void main(String[] args) {
String name;
//name = "Street"; // allowed
//name = "Some-Street"; // allowed
//name = "Street "; // not allowed
//name = " Street"; // not allowed
//name = "Street-"; // not allowed
//name = "-Street"; // not allowed
//name = "Street"; // allowed
//name = "1 st street"; // allowed
//name = "street 5"; // allowed
name = "111"; // NOT allowed
if (!Pattern.matches(PATTERN, name)) {
System.out.println("ERROR!");
} else System.out.println("OK!");
}
}
How do I add check if there is at least one character? 如何添加检查是否至少有一个字符?
No matter if it is at the beginning or end, or if there is space or dash between it and numbers. 无论是在开头还是结尾,或者它与数字之间是否有空格或短划线。 There just have to be at least one character.
必须至少有一个角色。
You can use this regex for your problem: 你可以使用这个正则表达式来解决你的问题:
^(?=.*\pL)[\pL\pN]+(?:[ -]+[\pL\pN]+)*$
For Java use: 对于Java使用:
final String regex = "^(?=.*\\pL)[\\pL\\pN]+(?:[ -]+[\\pL\\pN]+)*$";
RegEx Breakup: RegEx分手:
^
: Start ^
:开始 (?=.*\\pL)
: Using a lookahead make sure we have at least one unicode letter somewhere (?=.*\\pL)
:使用前瞻确保我们在某处至少有一个unicode字母 [\\pL\\pN]+
: Match one or more unicode letter or unicode digit [\\pL\\pN]+
:匹配一个或多个unicode字母或unicode数字 (?:
: Non-capturing group start (?:
非捕获组开始
[ -]+
: Match one or more space or hyphen [ -]+
:匹配一个或多个空格或连字符 [\\pL\\pN]+
: Match one or more unicode letter or unicode digit [\\pL\\pN]+
:匹配一个或多个unicode字母或unicode数字 )*
: Non-capturing group end. )*
:非捕获组结束。 *
means zero or more of this group. *
表示该组中零个或多个。 $
: End $
:结束 If I understand correctly, and according to what you've presented, you have the following conditions: 如果我理解正确,并根据您提供的内容,您有以下条件:
Based on these conditions, the following regex will work: 根据这些条件,以下正则表达式将起作用:
^(?![ -]|\d+$)[[:alnum:] -]+(?<![ -])$
To see this regex in use, click this link. 要查看正在使用的此正则表达式,请单击此链接。
This regex works as follows: 这个正则表达式的工作原理如下:
-
or space -
或空间
-
or space -
或空间
This will give you the following matches 这将为您提供以下匹配
Street
Some-Street
Street
1 st street
street 5
The regex will fail to match the following strings (as per your examples) 正则表达式将无法匹配以下字符串(根据您的示例)
Street
Street
Street-
-Street
111
Negative lookbehinds can sometimes cause issues in certain languages (like java). 负面的外观有时会导致某些语言(如java)出现问题。
Below is an adapted version of my previous regex that uses a negative lookahead instead of a negative lookbehind to ensure that the string doesn't end with hyphen -
or space 下面是我以前的正则表达式的改编版本,使用负前瞻而不是负向后观,以确保字符串不以连字符结尾
-
或空格 .
。
^(?![ -]|\d+$)(?:(?![ -]$)[\pL\pN -])+$
Following regex does the job: 正则表达式完成工作:
(?=.*[[:alpha:]])[[:alnum:]]{1}[[:alnum:] -]*[[:alnum:]]{1}
(?=.*[[:alpha:]]) part guarantees that alpha character [A-Za-z] exists inside word. (?=。* [[:alpha:]])部分保证单词字符[A-Za-z]存在于单词内。
[[:alnum:]]{1} part guarantees that string starts with alphanumeric character [A-Za-z0-9] [[:alnum:]] {1}部分保证字符串以字母数字字符开头[A-Za-z0-9]
[[:alnum:] -]* alphanumeric characters, space and dash characher might exist here. [[:alnum:] - ] *此处可能存在字母数字字符,空格和破折号字符。
[[:alnum:]]{1} part guarantees that string ends with alphanumeric character [A-Za-z0-9] [[:alnum:]] {1}部分保证字符串以字母数字字符结尾[A-Za-z0-9]
To see it live https://regex101.com/r/V0lesF/1 要现场直播https://regex101.com/r/V0lesF/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.