代码之家  ›  专栏  ›  技术社区  ›  Joe

使用gsub只提取特定长度的大写字母[重复]

r
  •  0
  • Joe  · 技术社区  · 11 月前

    我有一个字符串,我希望在其中提取国家代码,它将始终以大写字母和3个字符的形式出现。

    mystring
    "Bloggs, Joe GBR London (1)/Bloggs, Joe London (2)" 
    "Bloggs, Joe London (1)/Bloggs, Joe  GBR London (2)"  
    "Bloggs, Joe London (1)/Bloggs, Joe London (2)" 
    "Bloggs, Joe GBR London (1)/Bloggs, Joe GBR London (2)" 
     "Bloggs, J-S GBR London (1)/Bloggs, J-S GBR London (2)" 
    

    我想得到的

    mystring
    GBR/
    /GBR
    /
    GBR/GBR
    GBR/GBR
    
    Blanks are fine if there is no country, I can deal with them
    

    我尝试了一些我在这里看到的东西,其中一个试图删除所有不是大写的字符,但我留下了其他字母,我不想让它们像名字和位置中的大写字母一样。然后,我试图做类似的事情,试图删除所有以大写字母开头和结尾的字母(也因为名字问题而不高兴);

    gsub("[^A-Z$]", "", mystring)
    

    如果我只在有3个字母的地方保留所有大写字母,但我不能完全正确地编码,我想如果有人知道甚至知道一个更稳健的方法,它会像下面这样;

    gsub("[^A-Z$]{3}", "", mystring)
    
    1 回复  |  直到 11 月前
        1
  •  1
  •   Gregor Thomas    11 月前

    我喜欢 stringr::str_extract 用于从字符串中提取模式。这使您可以简单地输入所需的模式,而不是试图替换其他所有内容:

    mystring = c("Bloggs, Joe GBR London (1)/Bloggs, Joe London (2)", 
    "Bloggs, Joe London (1)/Bloggs, Joe  GBR London (2)"  ,
    "Bloggs, Joe London (1)/Bloggs, Joe London (2)" ,
    "Bloggs, Joe GBR London (1)/Bloggs, Joe GBR London (2)", 
     "Bloggs, J-S GBR London (1)/Bloggs, J-S GBR London (2)" 
    )
    
    ## extract first matches
    stringr::str_extract(mystring, "[A-Z]{3}")
    # [1] "GBR" "GBR" NA    "GBR" "GBR"
    
    ## or get all matches with `str_extract_all`
    stringr::str_extract_all(mystring, "[A-Z]{3}")
    # [[1]]
    # [1] "GBR"
    # 
    # [[2]]
    # [1] "GBR"
    # 
    # [[3]]
    # character(0)
    # 
    # [[4]]
    # [1] "GBR" "GBR"
    # 
    # [[5]]
    # [1] "GBR" "GBR"
    

    可以在基本R中使用 substring regmatches regexpr as seen in answers here .