Re: A quick Regex query

From: Date: Thu, 16 Feb 2006 23:38:04 +0000
Subject: Re: A quick Regex query
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-230607@lists.php.net to get a copy of this message
phplists wrote:
I am using the following: [0-9]{4} [A-Za-zÅØ]{2,20} to extract data like: 1000 Øslo How can I alter the above to limit to ONLY 4 digits, or in other words exclude: 11000 Beograd Please note that what I am extracting from is NOT at the begining of a line, so I can't use the ^ first.
Well, it depends what "usually is" (can be) before what you want to get and the regex family your using. If it's PCRE, you may try something like "\b\d{4} [A-Za-zÅØ]{2,20}" \b = "word boundary", it's basically a word separator and it's not included in the result you get (since it's more a "position" rather than a "string")
Also, you may notice a couple of 'special' characters in my expression 'Å + Ø' By putting them in they seem to work fine, but is this the best way of doing it?
I think it is. IIRC what you define in a "class" (i.e. between square-brackets) is quite fast; e.g. "[ab]" fastest than "(a|b)", is that what you were asking? -- Atentamente, J. Rafael Salazar Magaña Innox - Innovación Inteligente Tel: +52 (33) 3615 5348 ext. 205 / 01 800 2-SOFTWARE rsalazar@innox.com.mx http://www.innox.com.mx

« previous php.general (#230607) next »