Reputation: 393
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:
Upvotes: 0
Views: 176
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:
Upvotes: 3