stakowerflol
stakowerflol

Reputation: 1079

Get a word before regex from character variable

I want to get a variable name which is before in (?) characters.
For instance:
expect_equal("x", foo("select * from t where x in (?)"))

this is my try:

stmt <- "select * from t where x in (?)"
idxParam <- gregexpr("x in\\(?", stmt)[[1]]
substring(stmt, idxParam, idxParam + 1)

but when the length of the word is > 1 then it fails... Maybe there is a substring function which takes as parameters first letter of the word and finishes on space ?

Upvotes: 0

Views: 87

Answers (2)

s_baldur
s_baldur

Reputation: 33488

Using stringr:

library(stringr)
string <- "select * from t where x in (?)"
str_extract(string, "\\w+(?=\\s+in)")

This solution uses a positive lookahead to extract the first word that comes before \\s+in. Where \\s+ is any number (>0) of spaces.

Can easily be translated into base R:

sub(".+(\\w+)(?=\\s+in).+", "\\1", string, perl = TRUE)

EDIT: a non-regex solution that can also deal with many in's:

string_split <- strsplit(string, " ")[[1]]
string_split[which(string_split == "in") - 1]

Upvotes: 1

moodymudskipper
moodymudskipper

Reputation: 47300

Here's a way:

stmt <- "select * from t where x in (?)"
stmt_trimmed <- sub(" in \\(\\?\\)","", stmt) # [1] "select * from t where x"
tail(strsplit(stmt_trimmed," ")[[1]],1)
# [1] "x"

I don't know exactly what you tried to do but some mistakes were not escaping the ? and forgetting a space after in

Re: how about this: select * from t where x in (?) and y in (?)

x <- "select * from t where x in (?) and y in (?)"
pattern <- " ([^ ]+) in \\(\\?\\)"
raw_matches <- regmatches(x,gregexpr(pattern,x))
clean_matches <- gsub(pattern,"\\1",raw_matches[[1]])
clean_matches
# [1] "x" "y"

Upvotes: 1

Related Questions