我想匹配“3 T1F ROHITE01WMILWWI16”,其中只包含大写字母和数字.条件是它必须是两者.对于所有字母和所有数字,它不应该返回true.
测试用例:
> 3 T1F ROHITE01WMILWI16
> 3 T1F ROHITE01WMILwI16
> 3 T1F ROHITE01WMIL.I16
> 1234
> aaaa
> T1F ROHITH01WMILWI16
我正在使用^ [0-9 A-Z] $它正确匹配第一个和最后一个测试用例,但对于第四个测试用例,即1234,它也返回true.
解决方法
您可以使用
^([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允许字符
备选方案2与上面相同,但数字和大写字母字符类被交换以匹配1A或A1字符串.
> $ – 字符串结尾
因此,所需的最小字符串长度为2个字符.
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])需要一个数字.
s <- c("3 T1F ROHITE01WMILWI16","T1F ROHITH01WMILWI16") grep("^(?=[^A-Z]*[A-Z])(?=[^0-9]*[0-9])[0-9 A-Z]*$",perl=TRUE,value=TRUE)