I can't figure out how to write regex that matches these:
everyone hi
hi everyone
hi
But not this:
everyone hi everyone
The regex (?:everyone )?hi(?: everyone)?
will match the latter as well (which is not what I want). How to make such a regex? Or is it just not possible? I couldn't do enough research because I couldn't express the problem in correct words. Sorry if I posted a duplicate
Here is a brute force alternation way to get this done:
^(?:everyone +hi|hi(?: +everyone)?)$
RegEx Demo
RegEx Details:
^
: Start(?:
: Start a non-capture group
everyone
: Match everyone
: +hi
: Match 1+ spaces followed by hi
|
: ORhi
: Match hi
:(?: +everyone)?
: Optionally match 1+ spaces followed by everyone
)
: End non-capture group$
: EndYou could explicitly make a regex for each case (the first will capture two), utilizing beginning and end of line tokens
(^hi( everyone)?$)
(^everyone hi$)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With