Edvardoss
Edvardoss

Reputation: 393

Regular expression in R: How to cut sequence of digits?

There is a list of small texts. Some text in the ending may contain a sequence of numbers separated by spaces. For such cases, the first number of this sequence must be left in the text, cutting off the remaining sequence of digits. How to do it? Example:

  1. "Some words 1"-> "Some words 1"
  2. "Some words"-> "Some words"
  3. "Some 1 words 2" -> "Some 1 words 2"
  4. "Some 1 words 3 33 444" -> "Some 1 words 3"
  5. "Some words 544 11" -> "Some words 544"

Upvotes: 0

Views: 176

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521028

Use gsub() with the following pattern:

(\\d+)(?: \\d+)*$

Then replace with the first capture group, which is \\1.

x1 <- "Some words"
x2 <- "Some words 544 11"
x3 <- "Some 1 words 3 33 444"
result1 <- gsub("(\\d+)(?: \\d+)*$", "\\1", x1)
result2 <- gsub("(\\d+)(?: \\d+)*$", "\\1", x2)
result3 <- gsub("(\\d+)(?: \\d+)*$", "\\1", x3)

print(result1)
print(result2)
print(result3)

Demo here:

Rextester

Upvotes: 3

Related Questions