jmaccs64 Posted September 22, 2010 Share Posted September 22, 2010 I am horrible at Regular Expressions, I have been reading the tutorials, but maybe I read too much... I simply need to match alphabet Last Names A-He This is what I have so far... '^[a-h]' Any Help? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/214059-regular-expression/ Share on other sites More sharing options...
.josh Posted September 22, 2010 Share Posted September 22, 2010 if (preg_match('~^([a-g]|h([a-e]))~i',$name)) { // matched } Quote Link to comment https://forums.phpfreaks.com/topic/214059-regular-expression/#findComment-1113965 Share on other sites More sharing options...
Psycho Posted September 22, 2010 Share Posted September 22, 2010 if (preg_match('~^([a-g]|h([a-e]))~i',$name)) { // matched } Here is a breakdown of the pattern Crayon Violent supplied: ~^([a-g]|h([a-e]))~i ~ = delimiter ^ = match must start at the beginning of string [a-g] = letter must be 'a' through 'g' (because of the ^ the match has to be at the beginning of the string) | = OR h([a-e]) = letter must be an 'h' followed by 'a' through 'e' (because of the ^ the match has to be at the beginning of the string) ~ = delimiter i = matching will be case insensitive Quote Link to comment https://forums.phpfreaks.com/topic/214059-regular-expression/#findComment-1114099 Share on other sites More sharing options...
salathe Posted September 22, 2010 Share Posted September 22, 2010 For what it's worth, the parentheses around [a-e] are entirely superfluous and there is no real need for the other pair to be capturing anything. Quote Link to comment https://forums.phpfreaks.com/topic/214059-regular-expression/#findComment-1114100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.