Page 1 of 1

Matching strings with RegExp

Posted: Thu Jun 23, 2005 9:40 am
by visionmaster
Hello,

I want to match strings like

reutlingen
tübingen
Glashütte-Luchau
Glashütte - Luchau
Kurort Rathen
Weißbach b Königsbrück
Knappensee-Groß Särchen
Jämlitz-Klein Düben
Flughafen Leipzig/Halle
Stolzenhain a d Röder

So, allowed charachters are a to z, A to Z, ä, ü, ö, /, - and space (no other white space character!)

If the string holds any other characters or digits, there should be no match!

I thought character classes were the correct way, but I think I'm going the wrong direction...

Thanks for help!

Re: Matching strings with RegExp

Posted: Thu Jun 23, 2005 9:48 am
by visionmaster
Oops, find it out. It works like this:
^[-\\\sA-Za-zäöü]+$

Code: Select all

if (preg_match('/^[-\\\\\\sA-Za-zäöü]+$/si', $subject)) {
	
} else {
	
}
Any other suggestions?

Posted: Thu Jun 23, 2005 10:22 pm
by Ambush Commander
Try benchmarking a regexp that searches for a character that's not allowed. (add a ^ at the beginning of the []) I'm not exactly that knowledgable on optimizing regexps but this might be faster.