Home > Archive > PHP Language > February 2007 > Newbie needs help with ereg regular expression
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Newbie needs help with ereg regular expression
|
|
|
| Hi,
I have to repost this to restate the question. No disrespect to the original
reply.
Please can someone help regarding providing a regular expression for entering
a set of characters with letters, numbers, periods (.), hyphens (-), or
underscores (_) only.
I need to do this using ereg
I've got as far as this but it doesn't quite work:
$pattern="^([a-z0-9.-_]{0,30})$";
if(ereg($pattern,$_SESSION['id']))
....
Thank you in advance.
Kind regards,
Raj (newbie)
| |
| Captain Paralytic 2007-02-16, 7:59 am |
| On 16 Feb, 11:55, raj <r...@nospam.com> wrote:
> Hi,
>
> I have to repost this to restate the question. No disrespect to the original
> reply.
>
> Please can someone help regarding providing a regular expression for entering
> a set of characters with letters, numbers, periods (.), hyphens (-), or
> underscores (_) only.
>
> I need to do this using ereg
>
> I've got as far as this but it doesn't quite work:
>
> $pattern="^([a-z0-9.-_]{0,30})$";
> if(ereg($pattern,$_SESSION['id']))
> ...
>
> Thank you in advance.
>
> Kind regards,
>
> Raj (newbie)
As a matter of interest, why does it have to be done using ereg rather
than say, preg_match?
| |
|
| On Fri, 16 Feb 2007 11:59:27 +0000, Captain Paralytic wrote
(in article <1171627167.523089.186830@m58g2000cwm.googlegroups.com> ):
> On 16 Feb, 11:55, raj <r...@nospam.com> wrote:
>
> As a matter of interest, why does it have to be done using ereg rather
> than say, preg_match?
>
It's for 2 reasons
1. Be consistent throughout the site
2. I will disable preg_match and some other unused functions.
Any help with providing the ereg answer would be greatly appreciated.
Kind regards,
Raj
| |
| Captain Paralytic 2007-02-16, 8:00 am |
| On 16 Feb, 12:06, raj <r...@nospam.com> wrote:
> On Fri, 16 Feb 2007 11:59:27 +0000, Captain Paralytic wrote
> (in article <1171627167.523089.186...@m58g2000cwm.googlegroups.com> ):
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> It's for 2 reasons
>
> 1. Be consistent throughout the site
> 2. I will disable preg_match and some other unused functions.
>
> Any help with providing the ereg answer would be greatly appreciated.
>
> Kind regards,
>
> Raj- Hide quoted text -
>
> - Show quoted text -
Sheesh, what terrible reasons. the php manual itself advises the use
of perg_match over ereg for efficiency.
| |
| mvandenb@gmail.com 2007-02-16, 6:58 pm |
| On Feb 16, 6:55 am, raj <r...@nospam.com> wrote:
> Hi,
>
> I have to repost this to restate the question. No disrespect to the original
> reply.
>
> Please can someone help regarding providing a regular expression for entering
> a set of characters with letters, numbers, periods (.), hyphens (-), or
> underscores (_) only.
>
> I need to do this using ereg
>
> I've got as far as this but it doesn't quite work:
>
> $pattern="^([a-z0-9.-_]{0,30})$";
> if(ereg($pattern,$_SESSION['id']))
> ...
>
> Thank you in advance.
>
> Kind regards,
>
> Raj (newbie)
So you have written
$pattern="^([a-z0-9.-_]{0,30})$"
match a to z, 0 to 9, "." to '_' starting at the beginning of the line
and ending at the end of the line with a maximum of 30 character and a
minimum of 0 character.
they are right about preg_match. which all it means is you need to
wright you regex slightly differently and you can do fancy things if
you wanted to.
http://www.perl.com/doc/manual/html/pod/perlre.html
so
"/^[\w\.\-]{0,30}$/
or
"/^[a-zA-Z0-9\_\.\-]{0,30}$/
which is the same thing just longer to write out
and these are generally case sensitive remember so 'a' != 'A' (hence a-
zA-Z and not just a-z)
try that or at lest it should give you a good direction
| |
| Curtis 2007-02-17, 3:58 am |
| mvandenb@gmail.com wrote:
> On Feb 16, 6:55 am, raj <r...@nospam.com> wrote:
>
> So you have written
>
> $pattern="^([a-z0-9.-_]{0,30})$"
>
> match a to z, 0 to 9, "." to '_' starting at the beginning of the line
> and ending at the end of the line with a maximum of 30 character and a
> minimum of 0 character.
>
> they are right about preg_match. which all it means is you need to
> wright you regex slightly differently and you can do fancy things if
> you wanted to.
>
> http://www.perl.com/doc/manual/html/pod/perlre.html
>
> so
>
> "/^[\w\.\-]{0,30}$/
> or
> "/^[a-zA-Z0-9\_\.\-]{0,30}$/
> which is the same thing just longer to write out
> and these are generally case sensitive remember so 'a' != 'A' (hence a-
> zA-Z and not just a-z)
>
>
> try that or at lest it should give you a good direction
You should not escape dots in character classes, as they are not meta
characters in that context. Also, if - (dash) is the last character in
the character class, it also does not need to be escaped. Using \w
would be better, as it would allow for more characters with regard to
locale.
@OP: PCRE regex are much more efficient than the POSIX flavor. If your
worries are over consistency, then stop using POSIX regex. :-) As
already mentioned here, even the PHP documentation team is
recommending PCRE preg_* functions over ereg_* functions at all turns.
Also, you already have a pattern, have you tried testing it? If you
have a specific problem, ask a specific question.
--
Curtis
|
|
|
|
|