I want to validate strings that have the form:
One underscore _
, a group of letters in a
, b
, c
in alphabetical order and another underscore _
.
Examples of valid strings are _a_
, _b_
, _ac_
, _abc_
.
I can achieve the correct validation for most cases using the regex _a?b?c?_
, but that is still matched by __
, which I don't want to consider valid. How can I adapt this regex so that among my zero or one characters a?b?c?
, at least one of them must be present?
CodePudding user response:
You can add a (?!_)
lookahead after the first _
:
_(?!_)a?b?c?_
Details:
_
- an underscore(?!_)
- the next char cannot be a_
a?
- an optionala
b?
- an optionalb
c?
- an optionalc
_
- an underscore.
See the regex demo.
CodePudding user response:
You may use this regex with a positive lookahead:
_(?=[abc])a?b?c?_
RegEx Demo:
_
: Match a_
(?=[abc])
: Positive lookahead to assert that there is a lettera
orb
orc
a?b?c?
: Match optionala
followed byb
followed byc
_
: Match a_
PS: Positive lookahead assertions are usually more efficient than negative lookahead (as evident from steps taken on regex101 webste).