【问题标题】:Dutch postal codes regex R荷兰邮政编码正则表达式 R
【发布时间】:2020-09-13 01:27:49
【问题描述】:

我有向量 fromto

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。但是这个正则表达式似乎对我不起作用。

对于tofrom 的所有条目,我的尝试都给出了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

标签: r regex


【解决方案1】:

您需要将数字与\d 模式匹配,并在字符串文字"\\d" 中使用双反斜杠。此外,您不能在开头和结尾使用正则表达式分隔符/,您只需传递模式即可。

要使模式不区分大小写,您可以在模式的开头使用(?i),或者,在这里,只需使用[A-Za-z] 来匹配任何ASCII 字母。

你可以使用

str_detect(from, "^\\d{4}[A-Za-z]{2}$")

查看regex demoR demo

library(stringr)
from <- c("Valid from", "Zipcode from", "0000AA", "1798AA", "8900AA", "9167aa")
to <- c("Valid from", "Zipcode to", "1797zz", "8899ZZ", "9166ZZ", "9999ZZ")
str_detect(from, "^\\d{4}[A-Za-z]{2}$")
# => [1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE
from[str_detect(from, "^\\d{4}[A-Za-z]{2}$")]
# => [1] "0000AA" "1798AA" "8900AA" "9167aa"
 
str_detect(to, "^\\d{4}[A-Za-z]{2}$")
to[str_detect(to, "^\\d{4}[A-Za-z]{2}$")]
# => [1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE
# => [1] "1797zz" "8899ZZ" "9166ZZ" "9999ZZ"

【讨论】:

    猜你喜欢
    • 2013-07-27
    • 2021-10-13
    • 2020-01-05
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    相关资源
    最近更新 更多