PDA

View Full Version : Regular Expression Question



medic2000
October 14th, 2010, 10:07 PM
I have this regular expression:

preg_match("/^[A-Za-zıİάΦόφĞΗŞğηş\(\)\s]*$/", $word)

I want it to pass only letters, parentheses and one space between words. Space for words like "take heart".

But it allows more than one space(or whitespace) like "take \ \ \ heart".(this forum does not allow it so we can see only one whitespace now). What i need to change to make it correct?

spjackson
October 14th, 2010, 11:07 PM
I have this regular expression:

preg_match("/^[A-Za-zıİάΦόφĞΗŞğηş\(\)\s]*$/", $word)

I want it to pass only letters, parentheses and one space between words. Space for words like "take heart".

But it allows limited spaces like "take heart". What i need to change to make it correct?
Try


/^([A-Za-zıİάΦόφĞΗŞğηş\(\)]*\s?)+$/

medic2000
October 17th, 2010, 11:45 AM
Try


/^([A-Za-zıİάΦόφĞΗŞğηş\(\)]*\s?)+$/


Unfortunately it doesn't work. It still allows more than one whitespace :(

spjackson
October 17th, 2010, 11:55 AM
Unfortunately it doesn't work. It still allows more than one whitespace :(
Ah yes, sorry, so it would. Change the * after the ] to a + and that should solve it.

medic2000
October 17th, 2010, 12:16 PM
Ah yes, sorry, so it would. Change the * after the ] to a + and that should solve it.

This works great! Thank you so much! :)