【问题标题】:Change the position of string within a larger string in R更改字符串在R中较大字符串中的位置
【发布时间】:2021-03-02 11:12:58
【问题描述】:

我有一个字符列,如下所示:

names <- c("Isle North East", "London Northwestern", "Bedfordshire North East", "Southwark and Bermondsey North East",
           "Middlesbrough South and Cleveland East")

我想做的是将表达式 "North East" 移动到它之前出现的任何单词之前。因此,如果字符串是 XXXX XXXX North East,例如我希望它是“XXXX North East XXXX”。如果我展示我正在寻找的解决方案,也许会更容易:

ideal.names <- c("North East Isle", "London Northwestern", "North East Bedfordshire", "Southward and North East Bermondsey",
             "Middlesbrough South and Cleveland East")

我尝试了不同的方法,例如:

c(sub('^(.*) North East', 'North East \\1', names))

但这会返回以下内容:

[1] "North East Isle"                        "London Northwestern"                   
[3] "North East Bedfordshire"                "North East Southwark and Bermondsey"   
[5] "Middlesbrough South and Cleveland East"

所以问题是这会将字符串North East 移动到字符串的开头,而不是在它之前的单词之前。所以我得到North East Southwark and Bermondsey 而不是Southward and North East Bermondsey,这就是我想要的。 任何帮助都会很棒。 谢谢!

【问题讨论】:

    标签: r regex string stringr


    【解决方案1】:

    你可以使用

    gsub('(\\S+) (North East)', '\\2 \\1', names)
    ## => [1] "North East Isle"                        "London Northwestern"                    "North East Bedfordshire"                "Southwark and North East Bermondsey"   
    ##    [5] "Middlesbrough South and Cleveland East"
    

    详情

    • (\S+) - 第 1 组 (\1):一个或多个非空格
    • - 一个空格
    • (North East) - 第 2 组 (\2):North East 字符串。

    请参阅regex demo

    【讨论】:

    • 或者,gsub("(\\w+)(\\W+)(North East)", "\\3\\2\\1", names)(或 gsub("(*UCP)(\\w+)(\\W+)(North East)", "\\3\\2\\1", names, perl=TRUE) 以支持 Unicode),如果您需要处理单词之间的任何空格和标点符号。见this regex demo
    猜你喜欢
    • 2014-10-22
    • 2017-03-27
    • 2015-09-13
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 2020-03-28
    相关资源
    最近更新 更多