Reputation: 8297
I need to have a one positive regex which combines all 3 of the following:
-- ^(.*)\.abc\.(.*)$
-- ^(.*)\.abc$
-- ^abc\.(.*)$
such that the below cases do not apply to the regex:
-- 123abc
-- .abc123
-- 123abc.
and the following fgive the positive result:
--.abc
--.abc.
--abc.
I know that the 3 conditions can be in ORed in a group. I am looking fr a better solutoin, probably using lookaheads.
I tried using backreferencing : /^.*([\.]*)abc\1.*$/
but then this is positive for negative cases as well.
Upvotes: 1
Views: 113
Reputation: 784958
You can use this regex:
^(.*([.;]))?abc((?=[.;])\2.*)?$
RegEx Breakup:
^
: Start(.*([.;]))?
: start with anything followed by dot or semicolon (optional match). Note that we capture before abc
in capture group #2abc
: match abc
((?=[.;])\2.*)?
: End with same character we captured before abc
and anything following (optional match). Lookahead assertion is used to make sure we don't match empty back-reference \2
$
: EndUpvotes: 2