Regex 在r中的正则表达式内粘贴带引号的变量

Regex 在r中的正则表达式内粘贴带引号的变量,regex,r,Regex,R,我的问题的核心是使用paste将变量放入正则表达式中,并使用转义字符作为引号。这是其他stackoverflow问题的答案——但它似乎不起作用。我认为这个问题的独特之处在于它结合了这两个元素——使用粘贴将变量放入正则表达式中,并使用转义字符作为引号。我也尝试过使用cat,这是另一个常见的答案,但没有运气 我想做的是让它更容易通过数千个名字的列表按名字过滤。我正在从可视化软件Spotfire迁移到R,我想念列表框过滤器。对于如何执行这项任务,我非常乐意提供任何建议 是的,我对R编程和一般编程都是新

我的问题的核心是使用paste将变量放入正则表达式中,并使用转义字符作为引号。这是其他stackoverflow问题的答案——但它似乎不起作用。我认为这个问题的独特之处在于它结合了这两个元素——使用粘贴将变量放入正则表达式中,并使用转义字符作为引号。我也尝试过使用cat,这是另一个常见的答案,但没有运气

我想做的是让它更容易通过数千个名字的列表按名字过滤。我正在从可视化软件Spotfire迁移到R,我想念列表框过滤器。对于如何执行这项任务,我非常乐意提供任何建议

是的,我对R编程和一般编程都是新手。Stackoverflow一直是绝对最好的资源。你们一定是一群天才

多谢各位-

# mtcars example for filtering and finding names for the stack overflow question

data(mtcars)

# make the data match my dataframe, where I don't have row names but have a column with the name
mtcars$carname  <- NA  #declare the variable
mtcars$carname  <- rownames(mtcars) #assign the names to a column

findcar  <- function() {

  while(TRUE) {
    print("Type the car's name:")     
    apxname  <- readline() #approx name
    #type in Merc for this example
    carlst  <- mtcars$carname[(grepl("(apxname)",mtcars$carname, ignore.case = TRUE))] #list of cars that matches the approximate name
          # if I type   . . . (grepl("(Merc)", mtcars$carname, . . . )) it works great
    #So per other stackoverflow responses, I've tried using "paste" or "paste0" without success
    #I can't get this to work
    #carlst  <- mtcars$carname[(grepl(paste0('\"(',apxname, ')\"', sep=""),mtcars$carname, ignore.case = TRUE))]
    print("Here's the list of similar customers:")
    print(carlst)
    print("Type the number of your car:")
    carnum  <- readline()  #car number
    therightone  <- carlst[as.numeric(carnum)] 
    paste("You selected",therightone,"Is this the car (Y/N)?", sep=" ")
    carconf  <- readline()  #car confirmation
    if(carconf == "Y") break)
  }
return(therightone)
}

要解决特定问题,请将apxname作为变量而不是字符串引用。再清理一下,包括上面Mathias提到的打字错误,并将汽车确认提示打印出来:

此版本返回汽车名称;如果你想返回汽车的所有数据,你需要

findcar  <- function() {

    while(TRUE) {
        print("Type the car's name:")     
        apxname  <- readline() # approx name
        # list of cars that matches the approximate name
        carlst  <- mtcars[grepl(apxname, rownames(mtcars), ignore.case = TRUE),]
        print("Here's the list of similar customers:")
        print(carlst)
        print("Type the number of your car:")
        carnum  <- readline()  # car number
        therightone  <- carlst[as.numeric(carnum),] 
        print(paste("You selected",rownames(therightone),"Is this the car (Y/N)?", sep=" "))
        carconf  <- readline()  # car confirmation
        if(carconf == "Y") break
    }
    return(therightone)
}

“选择一个数字”提示可能会使用work,因为没有打印行号,但它至少可以工作。

请继续展开,但它似乎无法工作。谢谢。使用carlst我无法编辑它,因为它只是一个字符,但你还必须删除break语句后的括号。你不需要或不需要引号;您需要评估该变量。grepl也不需要括号;不管怎么说,它都会匹配的。您可能需要在其后面粘贴$以指示行尾,这样您就可以区分像马自达RX4和马自达RX4 Wag这样的东西。一般来说,您需要像greplpaste0'Mazda RX4'、'$'这样的东西,rownamesmtcars@alistaireOP似乎希望用户首先根据模糊匹配选择一个汽车品牌,然后提供所有可能的选择,从选项列表中选择精确匹配。因此,不需要区分不同的模型。
findcar  <- function() {

    while(TRUE) {
        print("Type the car's name:")     
        apxname  <- readline() # approx name
        # list of cars that matches the approximate name
        carlst  <- mtcars[grepl(apxname, rownames(mtcars), ignore.case = TRUE),]
        print("Here's the list of similar customers:")
        print(carlst)
        print("Type the number of your car:")
        carnum  <- readline()  # car number
        therightone  <- carlst[as.numeric(carnum),] 
        print(paste("You selected",rownames(therightone),"Is this the car (Y/N)?", sep=" "))
        carconf  <- readline()  # car confirmation
        if(carconf == "Y") break
    }
    return(therightone)
}