Reputation: 9644
I have sentences like these
word1 word2 abc word3 word4
word1 word2 word3 word4
And I want to get the matches:
abc
abc
or nothingabc
I need 3 different matches because $1 and $3 will be copied to the output as they are, while $2 should be modified. I expect $2 not always to be there, as well as $3).
My current solution is this one
(.+?(?=abc))(abc)?(.*)
This doesn't work if abc
is not part of the sentence. How can I express "match everything until that word if it exists" with regex?
P.S. I am using JS (not sure if it's relevant)
Upvotes: 1
Views: 588
Reputation: 626738
You may use
^(.*?)(?:(abc)(.*))?$
If the string can have line breaks, use
^([\w\W]*?)(?:(abc)([\w\W]*))?$
See the regex demo. In JS, [\w\W]
can be replaced with [^]
to match any chars including line break chars.
Details
^
- start of string(.*?)
- Group 1: any zero or more chars other than line break chars as few as possible(?:(abc)(.*))?
- an optional greedy pattern matching one or zero times:
(abc)
- Group 2: abc
(.*)
- Group 3: any zero or more chars other than line break chars as many as possible$
- end of string.Since the first group pattern is non-greedy, it will keep expanding from the start of the string until the first occurrence of the optional non-capturing group pattern (if present) or end of string, and if there is abc
, the second and third groups will be populated, else the whole string will land in Group 1.
Upvotes: 1