'R Regex expression with gsub
I am using gsub regex to select last part of expression
Example:
- "Bla-text-01" - I want -> "text-01"
- "Name-xpto-08" - I want -> "xpto-08"
- "text-text-04" - I want -> "text-04"
- "new-blaxpto-morexpto-07" - I want -> "morexpto-07"
- "new-new-new-bla-ready-05" - I want -> "ready-05"
I created this code that works with first 3 cases but now I have a new request to also work with 5 cases.
gsub(x = match$id,
pattern = "(.*?-)(.*)",
replacement = "\\2")
Can you help me?
Solution 1:[1]
x <- c("Bla-text-01",
"Name-xpto-08",
"text-text-04",
"new-blaxpto-morexpto-07",
"new-new-new-bla-ready-05")
sub("^.*-([^-]*-[^-]*)$", "\\1", x)
## [1] "text-01" "xpto-08" "text-04" "morexpto-07" "ready-05"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mikael Jagan |
