Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex pattern for underscore or hyphen but not both

Tags:

regex

I have a regular expression that is allowing a string to be standalone, separated by hyphen and underscore.

I need help so the string only takes hyphen or underscore, but not both.

This is what I have so far.

^([a-z][a-z0-9]*)([-_]{1}[a-z0-9]+)*$

foo             = passed
foo-bar         = passed
foo_bar         = passed
foo-bar-baz     = passed
foo_bar_baz     = passed
foo-bar_baz_qux = passed # but I don't want it to
foo_bar-baz-quz = passed # but I don't want it to
like image 762
Doctor06 Avatar asked Mar 10 '23 11:03

Doctor06


1 Answers

You may expand the pattern a bit and use a backreference to only match the same delimiter:

^[a-z][a-z0-9]*(?:([-_])[a-z0-9]+(?:\1[a-z0-9]+)*)?$

See the regex demo

Details:

  • ^ - start of string
  • [a-z][a-z0-9]* - a letter followed with 0+ lowercase letters or digits
  • (?:([-_])[a-z0-9]+(?:\1[a-z0-9]+)*)? - an optional sequence of:
    • ([-_]) - Capture group 1 matching either - or _
    • [a-z0-9]+ - 1+ lowercase letters or digits
    • (?:\1[a-z0-9]+)* - 0+ sequences of:
      • \1 - the same value as in Group 1
      • [a-z0-9]+ - 1 or more lowercase letters or digits
  • $ - end of string.
like image 114
Wiktor Stribiżew Avatar answered Mar 19 '23 09:03

Wiktor Stribiżew