正则表达式仅用于字母数字而不是数字

时间:2016-03-03 07:58:20

标签: regex r

我想匹配“3 T1F ROHITE01WMILWWI16”,其中只包含大写字母和数字。条件是它必须是两者。对于所有字母和全数字,它不应该返回true。

测试用例:

  • 3 T1F ROHITE01WMILWI16
  • 3 T1F ROHITE01WMILwI16
  • 3 T1F ROHITE01WMIL.I16
  • 1234
  • AAAA
  • T1F ROHITH01WMILWI16

我正在使用^ [0-9 A-Z] + $它正确匹配第一个和最后一个测试用例,但对于第四个测试用例,即1234也返回true。

4 个答案:

答案 0 :(得分:3)

您可以使用

^([0-9 A-Z]*[A-Z][0-9 A-Z]*[0-9][0-9 A-Z]*|[0-9 A-Z]*[0-9][0-9 A-Z]*[A-Z][0-9 A-Z]*)$

匹配

  • ^ - 字符串的开头

    然后来两个选择备选方案1
  • [0-9 A-Z]* - 0+允许的字符
  • [A-Z] - 一个大写的ASCII字母(强制性子模式)
  • [0-9 A-Z]* - 0+允许的字符
  • [0-9] - 数字
  • [0-9 A-Z]* - 0+允许的字符

    Alternative 2 与上述相同,但数字和大写字母字符类被交换以匹配1AA1字符串。
  • $ - 字符串结尾

因此,所需的最小字符串长度为2个字符

请参阅regex demoIDEONE demo

s <- c("3 T1F ROHITE01WMILWI16", "3 T1F ROHITE01WMILwI16", "3 T1F ROHITE01WMIL.I16", "1234", "aaaa", "T1F ROHITH01WMILWI16")
grep("^[0-9 A-Z]*[A-Z][0-9 A-Z]*[0-9][0-9 A-Z]*$", s, value=TRUE)

如果您需要支持任意长度的字符串,请使用this PCRE regex

^(?=[^A-Z]*[A-Z])(?=[^0-9]*[0-9])[0-9 A-Z]*$

(?=[^A-Z]*[A-Z])预测需要至少一个大写字母,(?=[^0-9]*[0-9])需要一个数字。

请参阅IDEONE demo

s <- c("3 T1F ROHITE01WMILWI16", "3 T1F ROHITE01WMILwI16", "3 T1F ROHITE01WMIL.I16", "1234", "aaaa", "T1F ROHITH01WMILWI16")
grep("^(?=[^A-Z]*[A-Z])(?=[^0-9]*[0-9])[0-9 A-Z]*$", s, perl=TRUE, value=TRUE)

答案 1 :(得分:2)

根据@akrun现在删除的答案,您可以查找包含大写字母和数字字符(带或不带空格)且不包含任何其他字符的字符串:

str1 <- c("3 T1F MILWWIHE01WMILWWI16", "3 T1F ROHITE01WMILWI16", "3 T1F ROHITE01WMILwI16", "3 T1F ROHITE01WMIL.I16", "1234", "aaaa", "T1F ROHITH01WMILWI16")

str1[grepl("[A-Z ]+", str1) & grepl("[0-9 ]+", str1) & !grepl("[^A-Z0-9 ]", str1)]
# [1] "3 T1F MILWWIHE01WMILWWI16" "3 T1F ROHITE01WMILWI16"    "T1F ROHITH01WMILWI16"

答案 2 :(得分:2)

您可以使用前瞻:

^(?=.*[0-9])(?=.*[A-Z])[0-9 A-Z]+$

str1 <- c("3 T1F MILWWIHE01WMILWWI16", "3 T1F ROHITE01WMILWI16", "3 T1F ROHITE01WMILwI16", "3 T1F ROHITE01WMIL.I16", "1234", "aaaa", "T1F ROHITH01WMILWI16")

grep("^(?=.*[0-9])(?=.*[A-Z])[0-9 A-Z]+$", str1, perl=TRUE, value=TRUE)
# [1] "3 T1F MILWWIHE01WMILWWI16" "3 T1F ROHITE01WMILWI16"    "T1F ROHITH01WMILWI16"

答案 3 :(得分:0)

您可以使用一个lookahead检查是否有\d个数字前面有任意数量的\D个非数字。
并匹配任何前面的任何数字空格+数字,后跟允许的字符。

^(?=\D*\d)[ \d]*[A-Z][ A-Z\d]*$

See demo at regex101multiline demo with .*? instead \D*

perl=TRUE一起使用并转到"^(?=\\D*\\d)[ \\d]*[A-Z][ A-Z\\d]*$"