如何在正则表达式中包含对象

时间:2022-06-23 13:50:01
namez <- c("foo2003", "bar2340", "naught45")
patternz <- "03"
grepl("[patternz]$",namez)

It does not work. What should I substitute [patternz] with, so the regular expression will match the contents of the patternz variable?

它不工作。我应该用什么来替换[patternz],以便正则表达式与patternz变量的内容相匹配?

[edit] Notice that I want to match the string "03", not the digits "0" and "3" separately.

[编辑]注意,我想要匹配字符串“03”,而不是数字“0”和“3”。

3 个解决方案

#1


10  

Must admit to struggling to see what the problem is here. For the example stated nothing more than

必须承认自己在努力寻找问题所在。对于这个例子来说,没有什么比这更重要的了。

R> namez <- c("foo2003", "bar2340", "naught45")
R> patternz <- "03"
R> grepl(patternz, namez)
[1]  TRUE FALSE FALSE

is required as patternz is a character vector and the aim is not to match 0 & 3 but to match the literal "03"

是必须的,因为patternz是字符矢量,其目的不是为了匹配0和3而是为了匹配文字“03”

If you need this to match only at the end of the strings, then we do need to add "$" either by hand:

如果您只需要在字符串末尾匹配这个,那么我们需要手工添加“$”:

R> patternz2 <- "03$"
R> grepl(patternz2, namez)
[1]  TRUE FALSE FALSE

or via a paste0() operation

或者通过paste0()操作

R> grepl(paste0(patternz, "$"), namez)
[1]  TRUE FALSE FALSE

The issue is to use patternz as the actual regexp and base R functions handle this perfectly.

问题是使用模式nz作为实际的regexp,并且基本的R函数可以很好地处理这个问题。

#2


5  

Looks like you need to create a character vector for grepl(), using paste0() seems to work, though is not that elegant:

看起来您需要为grepl()创建一个字符向量,使用paste0()似乎是可行的,但是不是很好:

> grepl(paste0("[", patternz, "]$"), namez)
[1]  TRUE  TRUE FALSE

#3


4  

package gsubfn is your friend

包裹gsubfn是你的朋友

library(gsubfn)
namez <- c("foo2003", "bar2340", "naught45")
patternz <- "03"
fn$grepl("[$patternz]$",namez)

#> fn$grepl("[$patternz]$",namez)
#[1]  TRUE  TRUE FALSE

Originally you indicated you wanted to match a 0 or a 3 at the end of the string. In your comment you allude to maybe wanting to match '03' in which case

最初,您表示希望匹配字符串末尾的0或3。在你的评论中,你提到可能想要匹配‘03’在这种情况下

fn$grepl("$patternz$",namez)

would be more appropriate.

可能更合适。

also

fn$grepl("`patternz`$",namez)

in this case might be more appropriate as the $ has double meanings.

在这种情况下可能更合适,因为$有双重含义。

#1


10  

Must admit to struggling to see what the problem is here. For the example stated nothing more than

必须承认自己在努力寻找问题所在。对于这个例子来说,没有什么比这更重要的了。

R> namez <- c("foo2003", "bar2340", "naught45")
R> patternz <- "03"
R> grepl(patternz, namez)
[1]  TRUE FALSE FALSE

is required as patternz is a character vector and the aim is not to match 0 & 3 but to match the literal "03"

是必须的,因为patternz是字符矢量,其目的不是为了匹配0和3而是为了匹配文字“03”

If you need this to match only at the end of the strings, then we do need to add "$" either by hand:

如果您只需要在字符串末尾匹配这个,那么我们需要手工添加“$”:

R> patternz2 <- "03$"
R> grepl(patternz2, namez)
[1]  TRUE FALSE FALSE

or via a paste0() operation

或者通过paste0()操作

R> grepl(paste0(patternz, "$"), namez)
[1]  TRUE FALSE FALSE

The issue is to use patternz as the actual regexp and base R functions handle this perfectly.

问题是使用模式nz作为实际的regexp,并且基本的R函数可以很好地处理这个问题。

#2


5  

Looks like you need to create a character vector for grepl(), using paste0() seems to work, though is not that elegant:

看起来您需要为grepl()创建一个字符向量,使用paste0()似乎是可行的,但是不是很好:

> grepl(paste0("[", patternz, "]$"), namez)
[1]  TRUE  TRUE FALSE

#3


4  

package gsubfn is your friend

包裹gsubfn是你的朋友

library(gsubfn)
namez <- c("foo2003", "bar2340", "naught45")
patternz <- "03"
fn$grepl("[$patternz]$",namez)

#> fn$grepl("[$patternz]$",namez)
#[1]  TRUE  TRUE FALSE

Originally you indicated you wanted to match a 0 or a 3 at the end of the string. In your comment you allude to maybe wanting to match '03' in which case

最初,您表示希望匹配字符串末尾的0或3。在你的评论中,你提到可能想要匹配‘03’在这种情况下

fn$grepl("$patternz$",namez)

would be more appropriate.

可能更合适。

also

fn$grepl("`patternz`$",namez)

in this case might be more appropriate as the $ has double meanings.

在这种情况下可能更合适,因为$有双重含义。