preg_match不能正常工作!? [英] preg_match doesn't work properly!?

查看:72
本文介绍了preg_match不能正常工作!?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能发现preg_match如何工作有问题虽然我不是

肯定。

假设你有一个你想要匹配的正则表达式字符串

的数字。您可以编写如下代码:

preg_match(''/ ^ [0-9] + $ /'',$ TestString);


OK一切似乎都很好。但是,你知道你是否通过了

跟随preg_match:" 12345 \ n"它会返回一场比赛

发生了什么?!?即使换行符不是我们的

正则表达式中的有效字符。


这是测试程序,*请运行如下所示的程序*:


<?php

$ TestString =" 12345 \ n";

print preg_match(''/ ^ [0-9] + $ /'',$ TestString);

?>


你会发现它打印1即使换行符不是''ta

正则表达式的有效部分。还有什么其他字符我想知道

可以放在正则表达式中并且字符串匹配!?对此有什么想法?b $ b想法?为什么PHP中存在这种未记录的行为?!?

对于正常表达式不能按预期工作或记录,似乎

就像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 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?

推荐答案

/'',


TestString);


好​​的一切似乎都没问题。但是,你知道你是否通过了

跟随preg_match:" 12345 \ n"它会返回一场比赛

发生了什么?!?即使换行符不是我们的

正则表达式中的有效字符。


这是测试程序,*请运行如下所示的程序*:


<?php
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";

打印preg_match(''/ ^ [0-9] +
TestString = "12345\n";
print preg_match( ''/^[0-9]+


这篇关于preg_match不能正常工作!?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆