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?
[edit] Notice that I want to match the string "03", not the digits "0" and "3" separately.
There are two ways to create a RegExp object: a literal notation and a constructor. The literal notation takes a pattern between two slashes, followed by optional flags, after the second slash.
Definition and Usage. The ?! n quantifier matches any string that is not followed by a specific string n. Tip: Use the ?= n quantifier to match any string that IS followed by a specific string n.
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .
The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.
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"
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With