doushu2699 2012-01-15 13:21
浏览 17
已采纳

奇怪的正则表达式问题

I think I may have overlooked something here, but I'm not sure why this strange behaviour is occurring.

I know this is a nonsensical regex capture, but my question is why is this occurring.

<?php
// < 15 "a" characters returns "aaaaaaaaaaaaaa {" (no matches)
$code = 'aaaaaaaaaaaaaa {';
// 15 "a" characters returns NULL
$code = 'aaaaaaaaaaaaaaa {';

$code = preg_replace('#(a+)+\{#', 'b', $code);
var_dump($code);
?>

I realise that the regex has a \s missing. If I change the regex to #(a+)+\s\{# I get "b". But why don't I get "aaaaaaaaaaaaaaa {" with 15 "a" characters instead of a NULL? The manual states that NULL is returned when an error occurs, but nothing is being printed out by PHP (error checking is on).

EDIT:

The reason I am using (a+)+ is because I am trying to simplify the problem down to the basic level. I started off with this:

I want to capture a series of non-whitespace characters followed by spaces followed by an opening brace.

this is some text {

So I used the regex:

#([^\s\{\}]+\s*)+\{#msi

Which also uses the double +. Is there a better way, and why is this causing an issue in the first place?

EDIT 2: From the answers below, it appears that the double + causes a lot of backreferences and should be avoided. I have opted for this [^\s\{\}][^\{\}]*+\{.

  • 写回答

1条回答 默认 最新

  • down00112 2012-01-15 13:32
    关注
    <?php
    
    ini_set('pcre.backtrack_limit',20000);
    $code = 'aaaaaaaaaaaaaaa {';
    $code = preg_replace('#(a+)+\{#', 'b', $code);
    var_dump($code, preg_last_error());
    
    ini_set('pcre.backtrack_limit',1000000);
    $code = 'aaaaaaaaaaaaaaa {';
    $code = preg_replace('#(a+)+\{#', 'b', $code);
    var_dump($code, preg_last_error());
    

    Result:

    NULL
    int(2)
    string(17) "aaaaaaaaaaaaaaa {"
    int(0)
    

    But you really should use either (a+) or (a)+, not a combination of both.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘