Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include object in regular expression

Tags:

regex

object

r

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.

like image 942
dmvianna Avatar asked Aug 31 '12 02:08

dmvianna


People also ask

How do you create an object in regex?

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.

What is ?! In regex?

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.

What is difference [] and () in regex?

[] 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 .

What does \\ mean in Java regex?

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.


1 Answers

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.

like image 129
Gavin Simpson Avatar answered Oct 07 '22 12:10

Gavin Simpson