Home > Software engineering >  Multiple zero or one, but at least one occurrence in regex
Multiple zero or one, but at least one occurrence in regex

Time:08-19

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 optional a
  • b? - an optional b
  • c? - an optional c
  • _ - an underscore.

See the regex demo.

CodePudding user response:

You may use this regex with a positive lookahead:

_(?=[abc])a?b?c?_

RegEx Demo

RegEx Demo:

  • _: Match a _
  • (?=[abc]): Positive lookahead to assert that there is a letter a or b or c
  • a?b?c?: Match optional a followed by b followed by c
  • _: Match a _

PS: Positive lookahead assertions are usually more efficient than negative lookahead (as evident from steps taken on regex101 webste).

  • Related