php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11461 \. after \- does not work
Submitted: 2001-06-13 05:25 UTC Modified: 2002-05-15 08:36 UTC
Votes:5
Avg. Score:4.4 ± 1.2
Reproduced:5 of 5 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (40.0%)
From: aweber at multamedio dot de Assigned:
Status: Not a bug Package: Regexps related
PHP Version: 4.0.6RC3 OS: Linux 2.2.16-SMP
Private report: No CVE-ID: None
 [2001-06-13 05:25 UTC] aweber at multamedio dot de
I tried to check email with
$check = ereg('^[0-9A-Za-z_\-\.]+@[0-9A-Za-z_\-]+\.[0-9A-Za-z_\-\.]+[0-9A-Za-z_\-]+$',$email);
This does not work with 'xxx-yyy@zzz.com', for example, althoug the regular expression is correct.  It works this way in any other programming language.

But if you write it in the following way it also works fine in PHP:
$check = ereg('^[\.0-9A-Za-z_\-]+@[0-9A-Za-z_\-]+\.[\.0-9A-Za-z_\-]+[0-9A-Za-z_\-]+$',$email);
Maybe the parser thinks of "-" as the range separator, althoug it is written as \- ?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-05-14 12:31 UTC] rasmus@php.net
 A  bracket  expression is a list of characters enclosed in
 `[]'.  It normally matches any single character  from  the
 list  (but  see  below).   If the list begins with `^', it
 matches any single character (but see below) not from  the
 rest of the list.  If two characters in the list are sepa?
 rated by `-', this is shorthand  for  the  full  range  of
 characters  between those two (inclusive) in the collating
 sequence, e.g. `[0-9]' in ASCII matches any decimal digit.
 It  is  illegal- for two ranges to share an endpoint, e.g.
 `a-c-e'.  Ranges  are  very  collating-sequence-dependent,
 and portable programs should avoid relying on them.

 To  include  a  literal `]' in the list, make it the first
 character (following a possible `^').  To include  a  lit?
 eral `-', make it the first or last character, or the sec?
 ond 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  excep?
 tion  of  these  and some combinations using `[' (see next
 paragraphs), all other special characters, including  `\',
 lose  their  special significance within a bracket expres?
 sion.
 [2002-05-14 16:05 UTC] postings dot php dot net at hans-spath dot de
@rasmus:

It sounds like you've mistaken "^" for "[^" ...

http://sunsite.utk.edu/gnu/regex/regex_17.html#SEC17
http://sunsite.utk.edu/gnu/regex/regex_23.html#SEC23
 [2002-05-14 19:30 UTC] rasmus@php.net
No I didn't.  The problem has nothing to do with ^ vs. [^
The question is whether \- is a valid way to get a literal - inside []'s
 [2002-05-15 07:49 UTC] alxwe at t-online dot de
It depends whether you use the Extended Regular Expressions (as PHP does at the moment) or the Advanced Regular Expressions which have much more features. Both are defined by POSIX.
Using ARE constructs like [0-9A-Za-z_\-\.] are right. If you want a "\" whithin the brackets you have to write "\\". The AREs are much easier to use and are more conclusive than the ERE.
 [2002-05-15 07:50 UTC] webmaster at linuxconfig dot de
Maybe you shoud include both types and let the user decide with of them he wants to use.
 [2002-05-15 08:36 UTC] rasmus@php.net
PHP does support ARE and again, this is not the issue here.
 [2002-05-15 10:07 UTC] webmaster at linuxconfig dot de
But using ARE, \- is a valid way to get a literal inside []'s
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Nov 24 09:01:23 2020 UTC