du8794 2011-04-28 15:49
浏览 12
已采纳

陷入正则表达式我不知道是否有可能使用php preg_match_all

i have a file out of which i want a specific data below is the sample data

 moduleHelper.addModule('REC');
  moduleHelper.addModule('TOP');

What i want is

anything.anything('x');i.e.

moduleHelper.addModule('');

The above is what i want to be returned .

i just dont want the 'x' part exclusive of single quote.

i tried by my self and wrote a regex which is below.

/(.*)\.(.*)\(\'[^.*]\'\)/mi

it gives me nothing according to the PCRE manual the ^ inside the [ ] does negation ??

  • 写回答

5条回答 默认 最新

  • doucang8303 2011-04-29 08:18
    关注

    It could be done with preg_replace_callback if you feel like figuring out how all that backreferencing works, but i think this is a bit easier:

    // the regex
    $regex = "/(?P<FIRST>.+)?\.(?P<SECOND>.+)\('(?P<PARAM>.+)?\'\)?/mi";
    
    $subject = <<<EOB
    moduleHelper.addModule('REC');
    moduleHelper.addModule('TOP');
    EOB;
    
    $matches = array();
    $numberOfMatches = preg_match_all($regex, $subject, $matches, PREG_SET_ORDER);
    
    
    $results = array();
    foreach($matches as $match)
    {
        array_push($results, sprintf("%s.%s('')", $match['FIRST'], $match['SECOND']));
    }
    
    print_r($results);
    
    // result:
    
    Array
    (
        [0] => moduleHelper.addModule('')
        [1] => moduleHelper.addModule('')
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失