1

I have a string that's formatted like:

x <- "\r\n\t\t\tExample\t\t\t"

I want to extract the "Example" in the middle, and I thought to do this by using gsub to match every backslash followed by a lowercase letter and replacing them with an empty string using gsub. I've tried a variety of patterns, checking that they match correctly on regex101.com, but whenever I put them into R, the output is exactly the same as the beginning string.

Here's the gsub calls with the expressions I've tried that have all failed to return anything but the original string:

y <- gsub("\\W[:lower:]", "", x)
y <- gsub("(\\W[:lower:])*", "", x)
y <- gsub("(\\[a-z])*", "", x)
y <- gsub("\\[a-z]*", "", x)
y <- gsub("\\W[a-z]", "", x)
y <- gsub("\\[a-z]", "", x)

I'm at my wit's end here. These patterns all seem to match what I need them to on the website, but they do nothing when plugged into gsub. My understanding of gsub is that it replaces every match in the string, not just the first, but this isn't even replacing the very first match. y always comes out identical to x.

Kai
  • 21
  • 5

1 Answers1

0

Use trimws to get rid of leading/trailing whitespace:

x <- "\r\n\t\t\tExample\t\t\t"
trimws(x)

See online demo

  • \r - is a CR symbol
  • \n - is an LF symbol
  • \t - is a tab symbol.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563