Reputation: 1885
I have following samples of strings
commit: February 2021 Master DS Commit c285fcbf59b698ec5695d35989a420da7659
commit: March market Sales Volume PFJan2021
commit: August market Input PFJan2021
commit: February 2021 Master DS Commit c5695d359820da7659
I want to extract words which are in between commit:
and Commit c285cb...
or the end of line. The result is as follows:
February 2021 Master DS
March market Sales Volume PFJan2021
August market Input PFJan2021
February 2021 Master DS
I understand that using following regex, I can extract words after commit:
.*commit: (.*)
I have tried another regex: .*commit: (.*)Commit.*
but it returns blank where Commit
word doesn't exist (I believe that's the expected behaviour).
Is there a way to use some style of or
condition which checks if Commit
word exists return words before it and after commit:
?
Upvotes: 1
Views: 97
Reputation: 627370
You can use
commit:\s*(.*?)(?:\s*Commit\b|$)
See the regex demo.
Details:
commit:
- a literal string\s*
- zero or more whitespaces(.*?)
- Group 1: any zero or more chars other than line break chars as few as possible(?:\s*Commit\b|$)
- either zero or more whitespaces and Commit
as a whole word or end of string.Upvotes: 2