Matching strings with RegExp

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Matching strings with RegExp

Post 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!
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Re: Matching strings with RegExp

Post 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?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
Post Reply