Bug #68810 [Com]: i guess i have found some problem with regular expressions
Edit report at https://bugs.php.net/bug.php?id=68810&edit=1
ID: 68810
Comment by: andrecamargotti at gmail dot com
Reported by: andrecamargotii at gmail dot com
Summary: i guess i have found some problem with regular
expressions
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Arch linux
PHP Version: 5.6.4
Block user comment: N
Private report: N
New Comment:
"The first matched because you didn't say the regex has to match the entire line, which it
won't do unless you explicitly tell it to with ^ and $ anchors."
your awnser doesn't make any sense to me. i don want to match the entire line, i want to match
the entire expresion.
assumes that the first script had the correct action is the same that assumes that any expresion can
have an underscore in somewhere and the script will ignore it.
new\s?[a-z0-9]+\s? it means that the expression must be the word new followed by an optinional blank
space followed by one or more alphanumeric char and followed by an optinional blank space.
underscore isn't an alphanumeric char, so the script can't print ok.
am i wrong?
Previous Comments:
------------------------------------------------------------------------
[2015-01-12 19:08:37] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
That's right: your regex wasn't accepting the underscore that was part of your input. The
first matched because you didn't say the regex has to match the entire line, which it
won't do unless you explicitly tell it to with ^ and $ anchors.
------------------------------------------------------------------------
[2015-01-12 12:30:42] andrecamargotii at gmail dot com
Description:
------------
sorry for the english
i wrote this script
$lines = file( $this->file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
foreach ( $lines as $line ) {
$numline = $numline+1;
if( preg_match( '/new\s?[a-z0-9]+\s?/i', $line)) { echo "ok $numline";}
}
}
and $this-file refers to an file where i wrote this:
new CR_a123)
the script print in the screen ok followed by number of the line (wrong)
if i change the script to this
$lines = file( $this->file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
foreach ( $lines as $line ) {
$numline = $numline+1;
if( preg_match( '/new\s?[a-z0-9]+\s?\)/i', $line)) { echo "ok $numline";}
}
}
the script don't print anything (right)
thanks for the help
the first script was worng because the expression have an underscore, and in script i especified
that can be only numbers and letters. the second script is correct because the script cannot output
ok.
i change the second script to:
$lines = file( $this->file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
foreach ( $lines as $line ) {
$numline = $numline+1;
if( preg_match( '/new\s?[a-z0-9_]+\s?\)/i', $line)) { echo "ok $numline";}
}
}
the third script prints ok (right)
now i have put underscore in the regular expression, so now the script can print ok.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68810&edit=1
Thread (5 messages)