1

I have a set of complex identifiers that are the column names of a dataframe. This regular expression matches all of them :

regex <- "^PLUTO-.{2}-.{4}-01.{1}-.{3}-.{4}-.{2}$"

Now I would like to generate random identifiers using this regex to get something like this :

"PLUTO-xx-xxxx-01x-xxx-xxxx-xx"
"PLUTO-xx-xxxx-01x-xxx-xxxx-xx"
...

Where x's are random characters.

Is there a function that does such things in R? I found such topics for java and python but nothing in R (only paste() and collapse() solutions).

1 Answer 1

1

One option is gsubfn

library(gsubfn)
gsub("\\^|\\$", "", gsubfn("\\.{([[:digit:]]+)}", ~ paste(rep("x", n), collapse=""), regex))
#[1] "PLUTO-xx-xxxx-01x-xxx-xxxx-xx"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks your code with rep(sample(c(LETTERS, 1:9), 1), n) did the trick! Though I was thinking about a function that would identify regex and randomly generate possible strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.