2010-01-18 64 views
1

我似乎無法得到這是什麼表情打算提取手柄:PHP正則表達式的幫助

preg_match("/^(?:[\s\*]*[email protected]([^\*\/]+?)\s(.+))/",$line,$match); 

$線是從一個文本文件中的線,而$匹配是一個數組

+0

你有什麼是$行的內容有任何想法? – 2010-01-18 16:35:34

+0

fireeyedboy拿到了文件中的文本結構右: /** * @class的TestClass * * @version 1.0 * @package TestTool * */ – KalenGi 2010-01-18 16:51:28

回答

6

這裏有一個解釋:

^    # match the beginning of the input 
(?:    # start non-capture group 1 
    [\s*]*?  # match any character from the set {'0x09'..'0x0D', '0x20', '*'} and repeat it zero or more times, reluctantly 
    @    # match the character '@' 
    (   # start capture group 1 
    [^*/]+?  #  match any character from the set {'0x00'..')', '+'..'.', '0'..'ÿ'} and repeat it one or more times, reluctantly 
)    # end capture group 1 
    \s   # match a whitespace character: [ \t\n\x0B\f\r] 
    (   # start capture group 2 
    .+   #  match any character except line breaks and repeat it one or more times 
)    # end capture group 2 
)    # end capture group 1 

的示例串的正則表達式將匹配是這樣的:* * *@abc asd

編輯:

我已經發布了用於生成上面的解釋解析器的測試版。它可以在這裏下載:http://big-o.nl/apps/pcreparser/pcre/PCREParser.html

+0

哇,你有一個工具來自動生成? :-) – Joey 2010-01-18 16:26:18

+0

是的,我寫了一個PCRE語法,並使用ANTLR創建了一個我用來創建這樣的* regex-explanation *的PCRE分析器/詞法分析器。 – 2010-01-18 16:29:02

+0

這絕對是驚人的!你有這個工具可供公衆使用嗎? – KalenGi 2010-01-18 16:35:09

0

這將匹配形式的字符串

** * ***@anything_that_is_not_an_asterisk_nor_a_slash anything else 

$match[1]包含"anything_that_is_not_an_asterisk_nor_a_slash"第一空間之前,$match[2]包含" anything else"

+0

這是一個很好的純英文解釋。我得到正則表達式的代碼實際上是試圖提取這種類型的字符串,但是因爲它在$ match [2] – KalenGi 2010-01-18 16:39:28

0

@讓我覺得該模式試圖捕獲電子郵件的元素... 作爲ROT總是記錄正則表達式。

+0

中選擇CRLF而造成混亂,我一開始也這麼想,但它匹配得多,更多。見Kenny和我的答案。 – 2010-01-18 16:31:52

+0

<[email protected]用戶名>,我認爲其含義實際上取決於輸入/上下文,線條是什麼樣子的? – 2010-01-18 16:34:20

2

可能試圖捕捉註釋塊這樣的(不包括第一次和最後一行)的行:

/** 
* @param $arg1 etc... 
* @return bool etc... 
*/ 
+0

這非常類似於源文本。我想知道的是它試圖以一步一步的方式來挑選,以便我可以看到它是怎麼搞的。 – KalenGi 2010-01-18 16:48:01