Reply to Re: Combining 2 preg matches.

Your name:

Reply:


Posted by Rik on 07/16/06 14:25

frizzle wrote:
> Chung Leong wrote:
>> frizzle wrote:
>>> Hi group,
>>>
>>> I have a function which validates a string using preg match.
>>> A part looks like
>>>
>>> if( !preg_match( '/^([a-z0-9]+(([a-z0-9_-]*)?[a-z0-9])?)$/',
>>> $string )
>>>>>
>>> preg_match( '/(--|__)+/' ,$string) ) {
>>>
>>> i wonder how i could combine those two into one ...
>>> I tried a few different options of putting the second match into the
>>> first one,
>>> using things like [^__]+ etc, but nothing worked for me.
>>> it should prevent double (or more) dashes or underscores behind each
>>> other.
>>> hello-there = ok
>>> hello--there != ok
>>>
>>> Any help would be great.
>>>
>>> Frizzle.
>>
>> What you need is a lookahead and lookbehind assertion on the dash and
>> underscore, stating that they're acceptable only if there're letters
>> in front and behind them:
>>
>> /^(?:[a-z]|(?<=[a-z])[-_](?=[a-z]))+$/
>
>
> wowowow, could you explain a little on this ?
> like the : and ?<= parts

non-capturing group (usefull when you just want to match, and don't need the
exact matched portion):
http://www.regular-expressions.info/brackets.html

positive lookbehind:
http://www.regular-expressions.info/lookaround.html

$regex ='/ #opening delimiter
^ #start of string
(?: #start of non-capturing group
[a-z] #any character between a and z
| #OR
(?<= #start of positive lookbehind (is preceeded by..)
[a-z] #any character between a and z
) #end of positive lookbehind
[-_] #character - or _ (not incorrect, but probably better
to [_\-],[_-] or [\-_]
(?= #start of positive lookahead
[a-z] #any character between a and z
) #end of positive lookahead
) #end of non-capturing group
+ #1 or more times, greedy
$ #end of string
/x';


Human translation:
The entire(1) string consists of 1 or more (2) characters [a-z] and possibly
the single characters _ or - enclosed by characters in the range [a-z].

(1) by achoring them with ^.....$
(2) by +

> (i assume 0-9 should still be included??)


If you want that, yes, just change every [a-z] to [a-z0-9].

Use the /i modifier if you want a match to be case-insensitive.

Grtz,
--
Rik Wasmus

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация