php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68810 i guess i have found some problem with regular expressions
Submitted: 2015-01-12 12:30 UTC Modified: 2015-01-12 19:08 UTC
From: andrecamargotii at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.6.4 OS: Arch linux
Private report: No CVE-ID: None
 [2015-01-12 12:30 UTC] 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.




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-12 19:08 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-01-12 19:08 UTC] 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-13 12:43 UTC] andrecamargotti at gmail dot com
"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?
 [2015-01-13 14:20 UTC] anon at anon dot anon
@andrecamargotti

The regex '/new\s?[a-z0-9]+\s?/i' when applied to 'new CR_a123)' correctly matches 'new CR', because that part consists of 'new' followed by a space followed by 2 letters or numbers, followed by no space (which was optional).

You haven't told the regex to match the '_a123' part, so it doesn't. If you want the regex to require that the word in the file ends at that point you will have to tell it so. $ would be suitable for that if there was nothing else on the line afterwards. In this case \b (word boundary) looks more suitable. I.e. '/new\s?[a-z0-9]+\b\s?/i'. You might not need the final \s? either.
 [2015-01-13 14:27 UTC] andrecamargotti at gmail dot com
hummm ok them, i understand the problem. many thanks for the help.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 13:00:01 2026 UTC