dpxw7293 2014-11-08 14:07
浏览 44
已采纳

首次出现时使用特定字符结束正则表达式

I have several source codes that I'm applying preg_match_all on.

this is what I tried:

$lazy = file_get_contents("Some_Source_code.txt");

if(!preg_match("#method_(.*)\(int var0, int var1, int var2\)#", $lazy, $function_name))
        die("nothing here");

preg_match_all("#method_".$function_name[1]."\(.*\){1}#", $lazy, $matches);

print_r($matches);

but the output comes like this:

Array
(
    [0] => Array
        (
            [0] => method_2393(int var0, int var1, int var2)
            [1] => method_2393(0, 0, 0)).equals(this.field_1351.getText().toString()))
        )

)

ok, what I want is $matches[0][1]. But How can I stop it once it detects the closing parentheses ' ) ' just like the first one.

I can process the line after I extract it, but how can I do it with regex? I searched the answers of similar problems but they were too specific.

  • 写回答

1条回答 默认 最新

  • download2014711 2014-11-08 14:08
    关注

    Modify the regex as

    #method_".$function_name[1]."\([^)]*\){1}#
    

    Where you got wrong

    #method_".$function_name[1]."\(.*\){1}#
    

    here you used \(.*\) where .* would match anything including the )

    Changes made

    • \([^)]*\) here [^)]* it matches anything other than ) so that it ends with the first occurence of the )

    • You can also use a lazy matching using .*? instead of .* which is gready and consumes as much as characters as it can

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

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择