I searched & tried below RegExp, but, not working for my requirement. Please, provide PHP RegExp, which accepts at least one alphanumeric and optional underscore or hyphen, but, Underscore or Hyphen should not repeat twice in a row.
/^([a-z0-9] -)*[a-z0-9] $/i
Example formats
- _test147
- test
- _a
- test_test
- test-test_, etc
CodePudding user response:
You may use this regex in ignore case mode:
^[-_]?[a-z\d] (?:[_-][a-z\d] )*[-_]?$
RegEx Details:
^: Start[-_]?: Match an optional_or-[a-z\d]: Match 1 of alphanumeric character(?:: Start a non-capture group[_-]: Match a_or-[a-z\d]: Match 1 of alphanumeric character
)*: End non-capture group. Repeat this group 0 or more times[-_]?: Match an optional_or-$: End
CodePudding user response:
/^([-_]?[a-z0-9] ) [-_]?$/i
This has a repeating sequence with an optional hyphen or underscore followed by alphanumerics, and then allows another optional hyphen or underscore at the end.
