简体   繁体   English

Java正则表达式检查是否至少有一个字母

[英]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]+)*$

RegEx Demo RegEx演示

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: 如果我理解正确,并根据您提供的内容,您有以下条件:

  • At least 1 letter 至少1个字母
  • Can contain digits (but only if the previous condition is met) 可以包含数字(但仅在满足先前条件时)
  • Dashes and spaces are allowed only if they are not at the beginning or end of the string 只有当它们不在字符串的开头或结尾时才允许使用破折号和空格

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: 这个正则表达式的工作原理如下:

  • Ensure the string doesn't begin with hyphen - or space 确保字符串不以连字符开头-或空间
  • Ensure the string isn't composed of only digits 确保字符串不仅由数字组成
  • Ensure the string contains between one and unlimited alphanumeric characters 确保字符串包含一个和不受限制的字母数字字符
  • Ensure the string doesn't end with hyphen - 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

Edit 编辑

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 -])+$

You can see this regex in use here 你可以在这里看到这个正则表达式

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.

相关问题 有没有办法用java递归检查字符串是否包含至少一个大写字母? - Is there a way to check if a string contains at least one capital letter recursively with java? 使用正则表达式查找文本文件中具有至少一个字母的所有关键字 - Find all keywords of a text file that have at least one letter by using regular expression Java中的拉丁字母正则表达式 - Latin letter regular expression in Java 如何检查 Java 字符串是否至少包含一个大写字母、小写字母和数字? - How do I check if a Java String contains at least one capital letter, lowercase letter, and number? Java 正则表达式检查 - Java Regular Expression check Java正则表达式,用于在任何位置包含至少一个字母数字字符的字符串 - Java regular expression for string containing at least one alphanumeric character, in any location 如何使用正则表达式将最后一个字母替换为java中的另一个字母 - How to replace last letter to another letter in java using regular expression 如何使用正则表达式检查Java中的字符串是否以至少一位数字结尾? - How do you use regular expressions to check if a String in Java ends with at least one digit? 使用正则表达式检查一个字符串至少包含一个Unicode字母 - Check a string contains at least one Unicode letter using regex 正则表达式检查单词是否有重复字母并防止输入字符或符号 - Regular expression to check in a word for repeated letter and prevent input of character or symbol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM