[英]Regular Expressions for string in php to find the word in commas
我正在嘗試為php寫一個Regexp來掃描真實文件,關鍵字是require
,而我想要的字符串在方括號"my string"
(需要保留)中
FILE1.TXT
require "testing/this/out.js"
require "de/test/as.pen"
require "my_love.test"
.....
print "I require coffee in the morning" //problem
require "_String_"
格式 FILE2.TXT
Class Ben extends Name
....
print "My good boss always extends my deadline" //problem
// looping true and determining if class or if reg file by folder structure
$subject = "Code above";
$pattern = '/^require+""/i'; // Not sure of the correct pattern
preg_match($pattern, $subject, $matches);
print_r($matches);
我只想testing this out
然后de/test/as.pen
返回第一個示例的數組。
這可能嗎? 會有很多問題嗎?
您可以使用此正則表達式:
$pattern = '/^ *require +"([^"]+)"/i';
^require (['"])([.\w /]+)\1
匹配結果:
preg_match('#^require (['"])([.\w /]+)\1#', $code, $match);
說明 :
^ # Start of string
require # reserved word with an space after
(['"]) # Quotations
( # Capturing group
[.\w /]+ # Any possible characters
) # End of capturing group
\1 # Same quotation
這個想法是跳過引號內的內容:
$pattern = <<<'LOD'
~
(["']) (?> [^"'\\]++ | \\{2} | \\. | (?!\1)["'] )* \1 # content inside quotes
(*SKIP)(*FAIL) # forces this possibility to fail
|
(?>^|\s)
require \s+ " ([^"]++) "
~xs
LOD;
preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
print_r($matches);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.