Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rearrange a string using regex

I have an expression as a string, like the following:

orig <- "mean(Sepal.Length, na.rm = TRUE)"
orig
#> [1] "mean(Sepal.Length, na.rm = TRUE)"

I would like to rearrange this string so that I get the following output:

"Sepal.Length$mean(na.rm = TRUE)"
#> [1] "Sepal.Length$mean(na.rm = TRUE)"

I know that I can use capture groups like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

gsub("(Sepal.Length)", "\\1\\$", orig)
#> [1] "mean(Sepal.Length$, na.rm = TRUE)"

but this doesn’t work to move text in the string:

gsub("(Sepal.Length)(.*)", "\\1\\$\\2", orig)
#> [1] "mean(Sepal.Length$, na.rm = TRUE)"

This question was helpful but the solution there is hardcoded, whereas here I don’t know the expression I will have at all, just that it will contain Sepal.Length. The expression above could be "sum(Sepal.Length)" for example.

I’m looking for a solution in base R only.

>Solution :

You can use the following pattern:

gsub("(.+)\\(Sepal\\.Length, *(.+)\\)", "Sepal.Length$\\1(\\2)", orig)
  • (.+) matches anything before the first parenthesis \\(;
  • Then, we always have "Sepal.Length". Note that . is a special character, so to use a literal dot you need \\.;
  • We then have a ,, maybe a space also * (* means "0 or more times");
  • After that we have the other arguments (.+), followed by the closing parenthesis \\).
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading