473,836 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help understanding an ereg expression

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...

Also, what does | | do? Normally, it'd mean 'or', but inside of []'s?
And two of them?

Jan 19 '06 #1
18 2028
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 (:

hth,

Steve
Jan 19 '06 #2
yawnmoth wrote:
if (eregi("^[a-z0-9| |\,\-\+\.]+$",$string)) Why does $string = 'te\st' yield a match? The ereg expression doesn't
have a \\ in it...


To have this expression make sence to me, it should look like

if (eregi("^[a-z0-9 ,+.-]+$",$string)) {

'-' should be the first og last character

all the special characters looses their meaning in a character class.
so you acually have 4 maching backslashes in the expression
Jan 19 '06 #3

"Steve" <Th*****@Aint.V alid> wrote in message
news:pa******** *************** *****@Aint.Vali d...
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.
\. 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.

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 (:

hth,

Steve

Feb 9 '06 #4


"yawnmoth" <te*******@yaho o.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Say I have the following script:

<?
$string = 'test';
if (eregi("^[a-z0-9| |\,\-\+\.]+$",$string))
^ means starting from the beginning of the line
[] means match one character in the set between the brackets.
+ after the [] means match 1 or more of those.
$ means must hit the end of the line.
{
echo 'matches!';
}
else
{
echo 'no match';
}
?>

Why does $string = 'te\st' yield a match? The ereg expression doesn't
have a \\ in it...

Also, what does | | do? Normally, it'd mean 'or', but inside of []'s?
And two of them?

Feb 9 '06 #5
"Jim Michaels" <jm******@nospa m.yahoo.com> wrote:
[...]
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.


"\s" means whitespace in a regular expression, but not in the string
being matched against.

P.
Feb 9 '06 #6

"Palle Hansen" <pa***@na.inval id> wrote in message
news:43******** *************** @dtext01.news.t ele.dk...
yawnmoth wrote:
if (eregi("^[a-z0-9| |\,\-\+\.]+$",$string))
Why does $string = 'te\st' yield a match? The ereg expression doesn't
have a \\ in it...


To have this expression make sence to me, it should look like

if (eregi("^[a-z0-9 ,+.-]+$",$string)) {

'-' should be the first og last character

all the special characters looses their meaning in a character class.


if that's true, how yould you match a tab character \t or a whitespace \s?
Somehow I don't think they would do that.
I tried <?php print preg_match('/[\s]/'," \t "); ?> and it returned 1.
it works. \t and \s works in a character class. you can escape things. I
don't know who told you this.
so you acually have 4 maching backslashes in the expression

Feb 9 '06 #7

"Steve" <Th*****@Aint.V alid> wrote in message
news:pa******** *************** *****@Aint.Vali d...
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.
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.
it's probably not safe to avoid escaping "-" because of its interpretation
as a range. better to be safe than sorry.

hth,

Steve

Feb 9 '06 #8

"Palle Hansen" <pa***@na.inval id> wrote in message
news:43******** *************** @dtext01.news.t ele.dk...
yawnmoth wrote:
if (eregi("^[a-z0-9| |\,\-\+\.]+$",$string))

Why does $string = 'te\st' yield a match? The ereg expression doesn't
have a \\ in it...


To have this expression make sence to me, it should look like

if (eregi("^[a-z0-9 ,+.-]+$",$string)) {

'-' should be the first og last character

all the special characters looses their meaning in a character class.
so you acually have 4 maching backslashes in the expression


nope. I just tried it. I've been able to escape things in RE char classes
for a long time. otherwise, you couldn't include things like tabs \t,
whitespace \s, newline \n, return \r and other control characters, word
characters \w, decimal digits \d. We didn't get Perl's character classes
like ::alpha:: (I don't think).
Feb 9 '06 #9
On 2006-02-09, Jim Michaels <jm******@nospa m.yahoo.com> wrote:

"Steve" <Th*****@Aint.V alid> wrote in message
news:pa******** *************** *****@Aint.Vali d...
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.
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
Feb 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2355
by: Stefan Gangefors | last post by:
I'm trying to figure out what I'm doing wrong when using ereg(). This is my regexp: ereg("^]$", "]"); and that does'n work, but this does: ereg("^$", "[");
3
20995
by: Martin Lucas-Smith | last post by:
Is there some way of using ereg to detect when certain filename extensions are supplied and to return false if so, WITHOUT using the ! operator before ereg () ? I have an API that allows as an input a regular expression, enabling the administrator to ensure a file upload matches a certain pattern. For instance, supplying the string '.exe$|.com$|.bat$|.zip$|.doc$'
0
2807
by: mcp6453 | last post by:
I am trying to use Jack's FormMail script (http://www.dtheatre.com/scripts/formmail). Since I'm brand new at PHP and not very good at HTML, I have an easy question, which I will narrow down. When the email arrives, it has this information: v_firstname: asdf v_lastname: asdf b_email: asdf@bellsouth.net v_phone: asdf v_cellphone: asdf
12
1916
by: hq4ever (at) 012 (dot) net (dot) il | last post by:
function testemail($email) { $validEmailExpr = "^(?)*@(?)*$"; return eregi($validEmailExpr, $email); } $email = "foo@bar.gov.mil"; testmail($email); //return TRUE
2
1221
by: Yannick Benoit | last post by:
Hi! I use this to find all links in a page : *(href)*=*(+) but recently i found out that it only works if links are setup this way: <a href = "... I would like to know if someone can help me find the proper expression which would work with <a href = "... <a href= "... <a href="... and <a href ="...
8
5484
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
6
1649
by: Cerebral Believer | last post by:
Is this a good ereg statement? if (!ereg("^+(\.+)*@+(\.{1,})*\.({2,}){1}$",$email_1 or $email_2)) { unset($_GET); $message_new = "<font color='#cc0000'>$email_1 or $email_2 is not a valid email address. Please try again.</font>";
4
1882
by: ollie.mitch | last post by:
Hi, I need two ereg expressions in my PHP code. One of them needs to check that a string only contains letters, and the other needs to check that the string only contains letters and commas (only one comma at each time). I thought that the code for only containing letters would be: eregi("^()", $keywords);
3
283
by: William Gill | last post by:
I am not to sharp on my regular expressions because I haven't used them in quite a while. So I am relearning regex and the PHP regex functions at the same time. Which means when I screw up, I'm not sure it's the regular expression that's wrong or the specifics of the PHP function's application thereof. I have a couple of questions, since I'm basically starting from scratch should I focus on the ereg* functions instead of the preg*...
0
9825
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9673
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10859
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10602
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6984
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4463
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.