Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

How to find any keyword in same regex in preg_match php

I have a text file and I want to search it like There are many lines in the file and I want to search each line, that it should return that line has the keyword "function" and that function should be called from the same line.

means I want to search keyword in same same regex. for eg. please check following test.txt if line 2 of txt file has function f3 but in that line there are no any calling function of f3, so that line should not be count. but line 3 of text file has function f1 and f1() also called from same line so it will be count.

the following is the file content from which I want to search lines.

test.txt

$xwbl209= "SN),AK mtyCcMXQHJ.T0-3qjfY5GnRl";
$k =5; function f3($a,$b,$c){ /*...*/ };
function f1(){ /*...*/ }; $a=1; $b=5; f1();
$aayw572 = f3($xwbl209{5},'',$xwbl209{10});
$k=10; function f2(){ /*...*/ }; $j=1; f1();
$bhzs038 = f3($xwbl209{5},$xwbl209{6},$xwbl209{8});
$aa = "aa"; function f4(){ /*...*/ }; $b=1; f4();
$b = "b"; function f5(){ /*...*/ }; $b=1; f4();
$aa = "aa"; function f6(){ /*...*/ }; $b=1; f6();
$bhzs038 = f3($xwbl209{5},$xwbl209{6},$xwbl209{8});

From the code below I was trying.

$lines = explode("
", file_get_contents('test.txt'));
$new_line = array();
foreach($lines as $line){
    if(preg_match(/somthing pattern/, $line)){
        $new_line[] = $line;    
    }
}
print_r($new_line);

I also tried the following pattern, but it doesn't work. /function ([^(])+(.*$1().*/g

The output of $new_line should be as follow.

function f1(){ /*...*/ }; $a=1; $b=5; f1();
$aa = "aa"; function f4(){ /*...*/ }; $b=1; f4();
$aa = "aa"; function f6(){ /*...*/ }; $b=1; f6();

Can you help me please? Thank you!!

question from:https://stackoverflow.com/questions/65917157/how-to-find-any-keyword-in-same-regex-in-preg-match-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One option could be using

functionh+(w+([^()]*)).*1;

Explanation

  • functionh+ A word boundary, match function and 1+ horizontal whitespace chars
  • ( Capture group 1
    • w+([^()]*) Match 1+ word chars and from (...)
  • ) Close group 1
  • .*1; Match in the same line what is captured in group 1 using a backreference 1 followed by ;

Regex demo

$lines = explode("
", file_get_contents('test.txt'));
$new_line = array();
foreach($lines as $line){
    if(preg_match('/functionh+(w+([^()]*)).*1;/', $line)){
        $new_line[] = $line;
    }
}
print_r($new_line);

Output

Array
(
    [0] => function f1(){ /*...*/ }; $a=1; $b=5; f1();
    [1] => $aa = "aa"; function f4(){ /*...*/ }; $b=1; f4();
    [2] => $aa = "aa"; function f6(){ /*...*/ }; $b=1; f6();
)

Or a shorter variation using preg_match_all:

$lines = file_get_contents('test.txt');
preg_match_all('/^.*?functionh+(w+([^()]*)).*1;/m', $lines, $m);
print_r($m[0]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...