【问题标题】:How to extract number, including all text before the number, from a string如何从字符串中提取数字,包括数字之前的所有文本
【发布时间】:2019-02-19 22:39:32
【问题描述】:

我有一个地址列表,其中包含 (1) 门牌号和 (2) 建筑物名称。我希望将字符串分成两列。棘手的部分是一些门牌号码包含字符,例如“贝克街 221B 号”。

下面的例子:

add <- c("5 Ark Royal House" , 
     "22A Blington Garden Lincoln Street", 
     "Flat 19 PICTON HOUSE" , 
     "2-3 Royal Albert Court" , 
     "Room 1 Grand Hall", 
     "No 17 The Dell Alpha House")

理想的结果如下所示:

aim <- data.frame("No"=as.character(c("5", "22A", "Flat 19", "2-3", "Room 1", "No 17")), 
              "Building" = as.character(c("Ark Royal House", 
                                          "Blington Garden Lincoln Street" , 
                                          "PICTON HOUSE", 
                                          "Royal Albert Court" , 
                                          "Grand Hall" , 
                                          "The Dell Alpha House")))

【问题讨论】:

    标签: r regex


    【解决方案1】:

    使用stringr

    library(stringr)
    lst <- str_match_all(add, "^(\\D*\\d[-\\w]*)\\s+(.+)")
    
    (aim <- setNames(as.data.frame(do.call(rbind, lst)),
                    c("all", "No", "Building")))
    

    或者在原版 R 中:

    pattern <- "^(\\D*\\d[-\\w]*)\\s+(.+)"
    lst <- regmatches(add, regexec(pattern, add, perl = T))
    (aim <- setNames(as.data.frame(do.call(rbind, lst)),
                     c("all", "No", "Building")))
    


    两者都会产生
                                     all      No                       Building
    1                  5 Ark Royal House       5                Ark Royal House
    2 22A Blington Garden Lincoln Street     22A Blington Garden Lincoln Street
    3               Flat 19 PICTON HOUSE Flat 19                   PICTON HOUSE
    4             2-3 Royal Albert Court     2-3             Royal Albert Court
    5                  Room 1 Grand Hall  Room 1                     Grand Hall
    6         No 17 The Dell Alpha House   No 17           The Dell Alpha House
    

    请参阅 regex101.com 上的 a demo for the expression

    【讨论】:

      【解决方案2】:

      基本方法,找到数字和名称之间的差距,用一个有希望的中性字符替换它(在本例中为_,但它可能是你知道不在任何地址中的任何东西),然后拆分那个角色。

      它假设包含数字的最后一个“单词”是“否”组件的结尾。如果这不适用于您的所有地址(适用于您的所有测试用例),那么这将不起作用。

      add <- c("5 Ark Royal House" , 
        "22A Blington Garden Lincoln Street", 
        "Flat 19 PICTON HOUSE" , 
        "2-3 Royal Albert Court" , 
        "Room 1 Grand Hall", 
        "No 17 The Dell Alpha House")
      
      split_add <- strsplit(gsub('([0-9\\-]+[0-9A-z]*) ', '\\1_', add), split='_')
      
      aim <- setNames(as.data.frame(do.call(rbind, split_add)),
        c('No', 'Building'))
      
      aim
      #>        No                       Building
      #> 1       5                Ark Royal House
      #> 2     22A Blington Garden Lincoln Street
      #> 3 Flat 19                   PICTON HOUSE
      #> 4     2-3             Royal Albert Court
      #> 5  Room 1                     Grand Hall
      #> 6   No 17           The Dell Alpha House
      

      reprex package (v0.2.1) 于 2019 年 2 月 19 日创建

      【讨论】:

        猜你喜欢
        • 2011-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-10
        相关资源
        最近更新 更多