You are here: Re: help understanding an ereg expression « PHP Programming Language « IT news, forums, messages
Re: help understanding an ereg expression

Posted by Jasen Betts on 11/17/89 11:40

On 2006-02-09, Jim Michaels <jmichae3@nospam.yahoo.com> wrote:
>
> "Steve" <ThisOne@Aint.Valid> wrote in message
> news:pan.2006.01.19.07.57.27.732286@Aint.Valid...
>> On Wed, 18 Jan 2006 21:36:20 -0800, yawnmoth wrote:
>>
>>> Say I have the following script:
>>>
>>> <?
>>> $string = 'test';
>>> if (eregi("^[a-z0-9| |\,\-\+\.]+$",$string))
>>> {
>>> echo 'matches!';
>>> }
>>> else
>>> {
>>> echo 'no match';
>>> }
>>> ?>
>>>
>>> Why does $string = 'te\st' yield a match? The ereg expression doesn't
>>> have a \\ in it...
>> the \ is escaping the s, so is invisible to the regular expression. To
>> have an \ in the string, you'd need to see \\
>
> That's not true. \s is whitespace. tabs or spaces.

That's not true the "\s" is outside of the regualar expression.
"\s" is the same as '\s' and prints as \s.

> \. means a "." as opposed to matching any character. don't know why they
> escaped the comma.
> \- was necessary so "-" is not interpreted as a range.
> \+ may not have been necessary, but safe anyway.

There are four \ in the regex. one is sufficient to cause a match.
in extended regex \ loses its special meaning inside bracket expressions.
(except its normal string meanings \t etc... where the \ insn't
actually present in the string anyway)

To include a literal '-' in a bracket expression have it first or last.

>>> Also, what does | | do? Normally, it'd mean 'or', but inside of []'s?
>>> And two of them?
>> Well, one | inside a class ( [...] ) matches |. Not too sure what 2 of
>> them mean, though. The space between them will also be matched. Using | as
>> an or would require the use of parentheses as well.

two are the same as one.

>> tbh, I'm not too sure the author really knew what they were doing, as
>> almost all those characters with special powers ( like \ ) lose them all
>> when between []'s (:

gotta agree there. possibly he thought he was writing a perl regex, where
he should have used preg_match.

Bye.
Jasen

 

Navigation:

[Reply to this 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

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