【发布时间】:2020-09-13 01:27:49
【问题描述】:
我有向量 from 和 to:
from <- c("Valid from", "Zipcode from", "0000AA", "1798AA", "8900AA", "9167aa")
to <- c("Valid from", "Zipcode to", "1797zz", "8899ZZ", "9166ZZ", "9999ZZ")
我想写一个str_detect 正则表达式,只有在有荷兰邮政编码时才给出TRUE(即,前四个数字,后两个字母。不区分大小写)。
我之前也发现过这个问题:Regular expression for Dutch zip / postal code。但是这个正则表达式似乎对我不起作用。
对于to 和from 的所有条目,我的尝试都给出了FALSE:
str_detect(test, "/^[1-9][0-9]{4} [a-z]{2}$/i")
str_detect(test, "^/d{4}?/w{2}$")
更多的尝试都失败了。先感谢您。可能遗漏了一些相当明显的东西:)
【问题讨论】:
-
/d必须是\\d。/不应在开头和结尾使用。试试str_detect(from, "^\\d{4}[A-Za-z]{2}$"),看看这个R demo。