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

Posted by Jim Michaels on 02/15/06 11:50

"Jasen Betts" <jasen@free.net.nz> wrote in message
news:ada.43ecf5d4.b12a7@clunker.homenet...
> 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 \\
>>>
>>>>
>>>> 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.
>>>
>>> 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 (:
>>
>>
>><?php print preg_match('/[\s]/'," \t "); ?>
>> 1
>
>> Where did you hear this? this has been a part of Perl RE's for a long
>> time.
>> Perl's REs were pretty much ported to PHP. PERL RE was POSIX if I am not
>> mistaken.
>
> Possibly they share a common ancestor. They are different now.
>
>> I've been able to escape things in [] character classes in UNIX and
>> grep-like programs for as long as I can remember. I thought I remember
>> having to so I could avoid weird behavior.
>
> I suspect you're mistaken.
>
> this from regex(7):
> .
> . To include a literal ]' in the list, make it the first character (fol-
> . lowing a possible ^'). To include a literal -', make it the first or
> . last character, or the second endpoint of a range. To use a literal
> . '-' as the first endpoint of a range, enclose it in [.' and .]' to
> . make it a collating element (see below). With the exception of these
> . and some combinations using [' (see next paragraphs), all other spe-
> . cial characters, including \', lose their special significance within
> . a bracket expression.
OK, I may be mistaken there about UNIX RegEx. I thought for sure I've used
them in grep and sed.

This is from the PHP manual:
Ranges operate in ASCII collating sequence. They can also be used for
characters specified numerically, for example [\000-\037]. If a range that
includes letters is used when caseless matching is set, it matches the
letters in either case. For example, [W-c] is equivalent to [][\^_`wxyzabc],
matched caselessly, and if character tables for the "fr" locale are in use,
[\xc8-\xcb] matches accented E characters in both cases.

The character types \d, \D, \s, \S, \w, and \W may also appear in a
character class, and add the characters that they match to the class. For
example, [\dABCDEF] matches any hexadecimal digit. A circumflex can
conveniently be used with the upper case character types to specify a more
restricted set of characters than the matching lower case type. For example,
the class [^\W_] matches any letter or digit, but not underscore.



>
>> it's probably not safe to avoid escaping "-" because of its
>> interpretation
>> as a range. better to be safe than sorry.
>
> depends on the definition of safe.
>
>
> 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

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