ID: 33334 Updated by: [EMAIL PROTECTED] Reported By: kloske at tpg dot com dot au -Status: Open +Status: Bogus Bug Type: Regexps related Operating System: Linux PHP Version: 4.3.10 New Comment:
Regular expressions are greedy by default. Change it to: $r_text = "(\"(([^\\\"]|\\\\|\\\")*?)\"|[^\",][^,]*?)"; or use the U modifier on the call and I bet it will do what you want. There is no bug here. Previous Comments: ------------------------------------------------------------------------ [2005-06-14 09:31:40] kloske at tpg dot com dot au Hi, Unfortunately the system this is running on at present is in production and I don't really have the resources just at this stage to get the latest stable snapshot up and running. Perhaps someone with this stable snapshot can copy and paste the 10 or so short lines into a test.php webpage and see if it runs as expected or not? If the reason you're asking me to do this is that you've tested it on the latest stable and it works then I will try as soon as I get time to check this, but otherwise I'll have to leave it a while as I have a lot of work on at the moment (buying a house, short staffed at work, serious spinal problems - the usual!) As a slight aside, I should mention that I just tested it on another PHP box which is totally unrelated to the first, this time OpenBSD, PHP 4.1.2 and it is also affected. I should have probably prefaced the report with the fact that I've got a workaround for my particular case which is an acceptable solution (just not accept strings which are unquoted and contain quotes). ------------------------------------------------------------------------ [2005-06-14 09:18:46] [EMAIL PROTECTED] Please try using this CVS snapshot: http://snaps.php.net/php4-STABLE-latest.tar.gz For Windows: http://snaps.php.net/win32/php4-win32-STABLE-latest.zip ------------------------------------------------------------------------ [2005-06-14 09:17:32] kloske at tpg dot com dot au Note that due to issues with the CAPTCHA, I've somehow included the wrong expected output and actual output. The ACTUAL output is: "some text","test \",thing" ("(([^\"]|\\|\")*)"|[^",][^,]*),("(([^\"]|\\|\")*)"|[^",][^,]*) array(5) { [0]=> string(27) ""some text","test \",thing"" [1]=> string(20) ""some text","test \"" [2]=> string(18) "some text","test \" [3]=> string(1) "\" [4]=> string(6) "thing"" } And the expected output is: "some text","test \",thing" ("(([^\"]|\\|\")*)"|[^",][^,]*),("(([^\"]|\\|\")*)"|[^",][^,]*) array(5) { [0]=> string(27) ""some text","test \",thing"" [1]=> string(20) ""some text"" [2]=> string(18) "some text" [3]=> string(1) "t" [4]=> string(6) ""test \", thing"" [5]=> string(6) "test \", thing" [6]=> string(1) "g" } Sorry for the confusion. ------------------------------------------------------------------------ [2005-06-14 09:14:29] kloske at tpg dot com dot au Description: ------------ Whilst trying to get a > 600 character regular expression to correctly match input lines from a file I discovered some strange mismatching which at first I imagined was a bug in my regex string until I reduced it to the simple test case included below. The test case shows some regex which should match limes that contain two fields, seperated by a comma. Each field is identical and can either be a string that does not start with a quote or a comma and contains no commas in it OR starts with a quote and ends with a quote and must contain only quotes or backslashes escaped with a preceeding backslash. Ie: Two fields which may only be simple strings or be c-style escaped strings seperated by a comma. Note in my expected output I am making an educated guess as to what the actual output would be, some of the other fields printed might be a little different. The basics of the problem however are clearly demonstrated. The final thing to note is that if I exclude quotes from the middle or end of the unquoted string case the problem vanishes. This leads me to suspect the problem is somehow related to regex's handling of quotes. Even if there are problems with my regex (I am well aware it is not optimal or particularly "good" in any sense - be aware this is a cut down test case only) this example clearly demonstrates php's regex engine matching a string which contains characters which are clearly excluded in the pattern which it matches. I've tested this with one field and it doesn't appear to be a problem there - it seems to only affect two fields one after another. Reproduce code: --------------- <?php $s = '"some text","test \",thing"'; $r_text = "(\"(([^\\\"]|\\\\|\\\")*)\"|[^\",][^,]*)"; $r_twofields = "${r_text},${r_text}"; preg_match("/^${r_twofields}\$/", $s, $line); echo "<pre>"; echo $s . "\n"; echo $r_twofields . "\n"; var_dump($line); echo "</pre>"; ?> Expected result: ---------------- "some text","test \",thing" ("(([^\"]|\\|\")*)"|[^",][^,]*),("(([^\"]|\\|\")*)"|[^",][^,]*) array(5) { [0]=> string(27) ""some text","test \",thing"" [1]=> string(20) ""some text","test \"" [2]=> string(18) "some text","test \" [3]=> string(1) "\" [4]=> string(6) "thing"" } Actual result: -------------- "some text","test \", thing" ("(([^\"]|\\|\")*)"|[^",][^,]*),("(([^\"]|\\|\")*)"|[^",][^,]*) array(5) { [0]=> string(28) ""some text","test \", thing"" [1]=> string(20) ""some text","test \"" [2]=> string(18) "some text","test \" [3]=> string(1) "\" [4]=> string(7) " thing"" } ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=33334&edit=1
