473,809 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preg_match doesn't work properly!?

I might have found a problem with how preg_match works though I'm not
sure.
Lets say you have a regular expression that you want to match a string
of numbers. You might write the code like this:
preg_match( '/^[0-9]+$/', $TestString );

OK everything seems fine. However, did you know if you pass the
following to preg_match: "12345\n" it will return that a match
occurred?!? Even though the newline is not a valid character in our
regular expression.

Here is the test program, *please run the program as written below*:

<?php
$TestString = "12345\n";
print preg_match( '/^[0-9]+$/', $TestString );
?>

You will find it prints 1 even though the newline character isn't a
valid part of our regular expression. What other characters I wonder
can be put in a regular expression and have the string match!? Any
ideas on this? Why is this undocumented behavior present in PHP?!?
For regular expressions to not work as expected or documented seems
like a pretty serious bug in PHP. I don't think there is a problem
with the regular expression.

Thoughts?
Jun 2 '08
13 5378
In our last episode,
<op************ ***@metallium.l an>,
the lovely and talented Rik Wasmus
broadcast on comp.lang.php:
On Tue, 27 May 2008 18:47:07 +0200, Lars Eighner <us****@larseig hner.com>
wrote:
>In our last episode,
<15*********** *************** ********@k30g20 00hse.googlegro ups.com>, the
lovely and talented ch************* ****@yahoo.com broadcast on
comp.lang.ph p:
>>I might have found a problem with how preg_match works though I'm not
sure. Lets say you have a regular expression that you want to match a
string of numbers. You might write the code like this: preg_match(
'/^[0-9]+$/', $TestString );
>>OK everything seems fine. However, did you know if you pass the
following to preg_match: "12345\n" it will return that a match
occurred?!?

Right, because it did.
>>Even though the newline is not a valid character in our regular
expression.

Doesn't matter. The whole expression matches before the newline.
>>Here is the test program, *please run the program as written below*:
>><?php
$TestString = "12345\n";
print preg_match( '/^[0-9]+$/', $TestString );
?>
>>You will find it prints 1 even though the newline character isn't a
valid part of our regular expression.

It returns 1 (a match exists) because all of the pattern is found
in $TestString. That is how perl regular expressions work.

preg_match('/dog/','catisnotadog bubba')
><SNIPPED more>
With all due respect, you're talking nonsense. You appartently missed that
the match is anchored to the start & end of string. Nothing of your story
has any relevance to the op's problem (which he already googled & solved
himself just before I answered him :) ).
$ matches the end of a line. When there is no newline, the end of a string
is presumed to be the end of a line. It was not ever anchored to "end of
string." Anyone who thinks of ^ and $ as relating to strings instead of
lines is asking for trouble.

--
Lars Eighner <http://larseighner.com/us****@larseigh ner.com
Countdown: 237 days to go.
Jun 2 '08 #11
On Tue, 27 May 2008 22:15:13 +0200, Lars Eighner <us****@larseig hner.com
wrote:
In our last episode,
<op************ ***@metallium.l an>,
the lovely and talented Rik Wasmus
broadcast on comp.lang.php:
>On Tue, 27 May 2008 18:47:07 +0200, Lars Eighner
<us****@larsei ghner.com>
wrote:
>>In our last episode,
<15********** *************** *********@k30g2 000hse.googlegr oups.com>,
the
lovely and talented ch************* ****@yahoo.com broadcast on
comp.lang.php :

I might have found a problem with how preg_match works though I'm not
sure. Lets say you have a regular expression that you want to matcha
string of numbers. You might write the code like this: preg_match(
'/^[0-9]+$/', $TestString );

OK everything seems fine. However, did you know if you pass the
following to preg_match: "12345\n" it will return that a match
occurred?! ?

Right, because it did.

Even though the newline is not a valid character in our regular
expression .

Doesn't matter. The whole expression matches before the newline.

Here is the test program, *please run the program as written below*:

<?php
$TestString = "12345\n";
print preg_match( '/^[0-9]+$/', $TestString );
?>

You will find it prints 1 even though the newline character isn't a
valid part of our regular expression.

It returns 1 (a match exists) because all of the pattern is found
in $TestString. That is how perl regular expressions work.

preg_match('/dog/','catisnotadog bubba')
><SNIPPED more>
>With all due respect, you're talking nonsense. You appartently missed
that
the match is anchored to the start & end of string. Nothing of your
story
has any relevance to the op's problem (which he already googled & solved
himself just before I answered him :) ).

$ matches the end of a line. When there is no newline, the end of a
string
is presumed to be the end of a line. It was not ever anchored to "endof
string." Anyone who thinks of ^ and $ as relating to strings insteadof
lines is asking for trouble.
/m
Tricks a lot of people, for obvious reasons.
'nuff said
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #12
Greetings, Lars Eighner.
In reply to Your message dated Wednesday, May 28, 2008, 00:11:01,
This is absurd. $ matches the end of the line. You see that is why a
"newline" is called a newline. It is after the end of the line.
$ matches the end of the line while it set to the multiline. Otherwise it
matches the end of *string* (or right before the last \n at the end of string).
Feel the difference.
--
Sincerely Yours, AnrDaemon <an*******@free mail.ru>

Jun 2 '08 #13
Lars Eighner a écrit :
Anyone who thinks of ^ and $ as relating to strings instead of
lines is asking for trouble.
Or is reading documentation carefully :p

Regards,
--
Guillaume
Jun 2 '08 #14

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

Similar topics

1
2538
by: Station Media | last post by:
Hi, here my problem, i use stored procedure in ACCESS database(latest version), and i would like to use this procedure but it doesnt work, do you know why ? Procedure: PARAMETERS MyField Text ( 255 ); SELECT DISTINCT MyField AS Result FROM tb_Client;
3
7226
by: Clouds | last post by:
Hi ! How do I add the dynamic event handler for a dropdownlist present in the itemtemplate of a datalist !! I am doing it in the itemdatabound event of the datalist but it doesnt work... I am also setting the autopostback property to true for the dropdown list and it works but the handler doesnt get invoked at runtime... I have to do it in itemdatabound becaz whether to add the handler or not is driven based on the information which i...
2
2834
by: effendi | last post by:
Hi I tested the following function in Safari and it doesnt work. This is tested fine in IE. function processOutcome(){ mainDatabase=document.forms.AssessDatabase.value var oCheckboxs=document.forms.TeamID if (oCheckboxs=="undefined" )
0
1650
by: Juna | last post by:
I have been working in vs2003, but now started to work in vs2005 but the problem, I have simple web application not website, which work i mean open in browser when we press F5 or run the application by pressing run button. but it doesnt work when we type the path directly in the browser's address bar eg. http://localhost/testapplicationdefault.aspx error it gives is "COULD NOT LOAD testapplication._default". IN default.aspx. While in...
1
2087
Digital Don
by: Digital Don | last post by:
I am writing a program for Peg solitaire... To check for no repetition of previous states I use a Set for storage of Board states.. The pronblem is when I declare the set as type char i.e. set <char> open it doesnt take any char values into it and says cannot convert char to const char... I wonder why is it converting char to const char when I have declared that it shud be of type char...It doesnt work with int either,.. Please help...
3
2986
by: jx2 | last post by:
hi guys i would appriciate your coments on this code - when i ran it for the very first time it doesnt see @last = LAST_INSERT_ID() but when i ran it next time it read it properly i need to know it imiedietely after i insert value into session1... is there any other way to do it? insert into 2 tables at the same time ...? if(!($sessid)){ session_register('sessid'); mysql_query("insert into session1 set sessid=null,
1
1601
by: Dany13 | last post by:
hi all. i using some text box for input value and some localvarible for passing this data to dataset . give instance for correct row of dataset and data in data table . use one gird view for showing curent data in dataset . in end i am calling update metod to insert data in sql database but this metod doesnt work correctly. at all doesnt work. but givenot any error . fill data from my database (work propebly) ...
20
2883
by: Hush | last post by:
Hi, The following code works fine in IE7 but FF returns with an error: Access denied to achieve the property Element.firstChild. In this line: nodes = xmlDoc.documentElement.childNodes; My code:
9
1705
by: AGP | last post by:
I've been scratching my head for weeks to understand why some code doesnt work for me. here is what i have: dim sVal as string = "13.2401516" dim x as double x = sVal debug.writeline ( x)
0
9721
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
9601
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
10637
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
10379
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,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
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
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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.