duanou1904 2016-02-13 15:27
浏览 29

正则表达式在函数体中找到某些单词

I want to write a regular expression to find the word after $body->
That is userId, deviceId, deviceToken and deviceType in the following code sample:

 function deviceUpdate($body)
 {  
    $userId=trim($body -> userId);
    $deviceId= $body -> deviceId;
    $deviceToken=trim($body -> deviceToken);
    $deviceType=trim($body -> deviceType);
 }

There can be a single space or multiple spaces after body keyword and after ->

The regular expression I tried is:

(?<=body ->\s)\S+

But this does not work properly if there is no space between body and ->.

Edit : I used the the regular expression ~function\s*(.+?)(~ to find the name of function ( deviceupdate) and ~\$body\s*->\s*(\w+)~ to find all keywords after body -> operator. It works perfectly fine. Suppose, i have 2 functions :

function deviceUpdate($body)
 {  
    $userId=trim($body -> userId);
    $deviceId= $body -> deviceId;

 }

function postad($body)
 {

    $adtitle = $body -> adtitle;
    $addescription = $body -> addescription;

  }

How to extract the function 1 name that is deviceUpdate and corresponding keywords in that same function after body -> that is ( userId,deviceId) in one array and function 2 name postad and corresponding keywords in that same function after body -> that is ( adtitle,addescription) in second array?

Currently when i use preg_match_all and regular expression as stated above it returns everything it matches in a single array.

  • 写回答

2条回答 默认 最新

  • dongwo5686 2016-02-13 15:41
    关注

    In your solution, you are using a positive lookbehind that, in PHP PCRE regex, cannot be of undefined length.

    If you have cases where $body and -> are separated with zero or more spaces, you cannot rely on a fixed-width lookbehind. Use capturing and grab the submatch from the match object (to only match alphanumerics with underscore to avoid matching );, you need to replace \S with \w):

    ~\$body\s*->\s*(\w+)~
    

    See the regex demo

    See IDEONE demo:

    $re = '~\$body\s*->\s*(\w+)~'; 
    $str = " function deviceUpdate(\$body)
     {  
        \$userId=trim(\$body -> userId);
        \$deviceId= \$body -> deviceId;
        \$deviceToken=trim(\$body -> deviceToken);
        \$deviceType=trim(\$body -> deviceType);
     }"; 
    preg_match($re, $str, $matches);
    echo $matches[1]; // => userId
    

    If you need all matches, use preg_match_all:

    preg_match_all($re, $str, $matches);
    print_r($matches[1]);
    

    See another IDEONE demo

    To address the edit:

    You can use

    (?:\bfunction\s+(\w+)\([^()]*\)\s*\{|(?!^\G))(?:(?!\bfunction\s+\w+\([^()]*\)\s+{|}\h*?
    ).)*?\$body\s*->\s*(\w+)
    

    See the regex demo

    In PHP, use it as

    $re = '~(?:\bfunction\s+(\w+)\([^()]*\)\s*\{|(?!^\G))(?:(?!\bfunction\s+\w+\([^()]*\)\s+{|}\h*?
    ).)*?\$body\s*->\s*(\w+)~s'; 
    

    See the PHP demo

    The regex above uses a custom boundary specified with (?:\bfunction\s+(\w+)\([^()]*\)\s*\{|(?!^\G)) and a tempered greedy token (?:(?!\bfunction\s+\w+\([^()]*\)\s+{|}\h*? ).)* making sure a match is only found inside one function

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)