1

Given the following string:

Test <- c("123 - Test1", "1234 - Test2", "123 - 45 - Test3")

When I use gsub as below with very basic regex

gsub(".*- ", "", Test)

I get the following output:

"Test1" "Test2" "Test3"

How can I change my regex to only substitute up until the first - to get the following result:

"Test1" "Test2" "45 - Test3"

I know regex is 'greedy' so I am looking for a way to overcome this greediness.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Schalk Burger
  • 58
  • 1
  • 11
  • 2
    `sub(".*?- ", "", Test)`, or `"^[^-]*- "`. Note that `sub` will remove the first occurrence only, `gsub` removes all. Also, see [the R related answer](https://stackoverflow.com/questions/32767164/use-gsub-remove-all-string-before-first-white-space-in-r) where you just need to use the hyphen. – Wiktor Stribiżew Oct 02 '19 at 06:51
  • Thanks @WiktorStribiżew. Should I remove this question because it is a duplicate? – Schalk Burger Oct 02 '19 at 06:54
  • 2
    No need to remove it. Just mind that [`greedy` and `lazy`](https://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-expressions) are too well-known terms in the regex world. – Wiktor Stribiżew Oct 02 '19 at 06:57

0 Answers0