2017-03-31 56 views
0
echo '<pre>'; print_r(1); echo '</pre>'; <- select this 

echo '<pre>'; print_r($temp); echo '</pre>'; <- select this without space 

// echo '<pre>'; print_r($temp); echo '</pre>'; <- skip this 



echo '<pre>'; print_r($temp3); echo '</pre>'; <- select this 

print_r($temp1); <- select this 

// print_r($temp11); <- skip this 

    print_r($temp2); <- select this without spaces 

    if (true) { 
     print_r($temp4); <- select this without spaces 

    } 

我有print語句代碼,我需要選擇唯一的非註釋語句正則表達式選擇不評論語句

這裏是一個正則表達式

^((?!\/\/\s?))(echo '<pre>';\s?)?(.+)?(print_r)(\.?)(\w+)?\((.+)?\);(echo '<\/pre>';)? 

DEMO

如何跳過空格? ' print_r($temp4);' 或如何更改正則表達式使其工作?

+0

爲什麼所有的捕獲組?你想要捕捉什麼?爲什麼?這只是一個非常簡單的問題,因爲它不清楚你想要做什麼。而且,空格修剪就像在外部捕獲組中添加'\ s *'一樣簡單。 – sln

+0

我試圖選擇「echo'

'; print_r($temp); echo '
';」或者print_r($ temp2);但沒有評論 – David

+0

仍然不清楚'回聲'

'; print_r(//' <- skip this ? '\n$temp3); echo '
';' – sln

回答

1
import re 

rgx = re.compile('^[ \t]*((echo|print_r).*;)', re.MULTILINE) 
for line in rgx.finditer(code): 
    print line.group(1) 

# echo '<pre>'; print_r(1); echo '</pre>'; 
# echo '<pre>'; print_r($temp); echo '</pre>'; 
# echo '<pre>'; print_r($temp3); echo '</pre>'; 
# print_r($temp1); 
# print_r($temp2); 
# print_r($temp4); 
+0

我需要選擇「echo'

'; print_r($temp); echo '
';」或者print_r($ temp2);作爲一個選擇 – David

+0

好吧,答案已更新。 – feqwix

+0

這幾乎是我所需要的,但它沒有選擇,而不是空格選項卡我的意思是如果代碼縮進與選項卡它跳過選擇可以修改正則表達式一點點,使其與標籤太 – David

0

也許是這樣的:

^(?!\/\/) *(echo '<pre>';\s?)?(.+)?(print_r)(\.?)(\w+)?\((.+)?\);(echo '<\/pre>';)? 
+0

這個正則表達式還包括完全匹配空格 – David

0

此正則表達式不會捕捉行,如果有一個評論(//, @*, /*, <!--)否則將捕捉一切沒有空格,希望這能解決您的問題。

([\n]|^)(?P<group>(?! *\/\/| *@\*| *\/\*| *<!--| *\\\*)([^\n]*?)(\S)(((?! *\/\/| *@\*| *\/\*| *<!--| *\\\*).)*)) 
+0

你測試你的正則表達式嗎? https://regex101.com/r/zwUDGI/1 – David

+0

@David ty爲提示,我還沒有測試過,現在要編輯 – Paz

+0

這裏是你的正則表達式https://regex101.com/r/IgvpGV/ 1 – David

0

這裏是我的解釋你想要的東西: Regex101

"^(?!\s*?//)\s*?(print_r\(.*\);?|echo\s*?'<pre>.*;).*$"gm

你可以在它做什麼的web應用的側面充分說明