Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for one character only

Tags:

regex

I need a regular expression for a string with exactly one alphabetic character in it.

For example, it should match:

  • 8*x=16
  • x+2=2
  • u329398239823
  • 43044343k43304403

...but not:

  • xsfdjfefljefe
  • 44434f9434343 f
  • 98(***(*)))(*)(0((-900-87
  • 32233232
like image 201
user1461607 Avatar asked Apr 10 '13 14:04

user1461607


1 Answers

/^[^a-z]*([a-z])[^a-z]*$/i

i for case-insensitive matching.

^ and $ anchors to force the regex to match the entire string.

( ) to capture the letter.

like image 175
Loamhoof Avatar answered Oct 01 '22 12:10

Loamhoof