Thomas
Thomas

Reputation: 143

Regex - special character surrounded by letters or space

I want to check that an underscore is obligatorily between two letters or spaces.

For example :

"-ayui" : not good

"jjdk-jk" : good

"hd -sdjh" : good

"fgggdf-" : not good

"hjhfs - jgkd" : good

" kf8-1dd" : not good

Can you help me ?

Thanks very much !

Upvotes: 0

Views: 1732

Answers (1)

The fourth bird
The fourth bird

Reputation: 163277

Looking at your example data, I think you mean hyphen instead of underscore.

If that is the case, you could use ^[a-z]+\s*-\s*[a-z]+$ to match:

  • From the beginning of the string ^
  • One or more characters [a-z]+
  • Zero or more times a whitespace \s*
  • Match a hyphen -
  • Zero or more times a whitespace \s*
  • One or more characters [a-z]+
  • Until the end of the string $

Upvotes: 3

Related Questions