2015-09-26 89 views
2

對不起,我是PHP新手。我試圖匹配一個字符串的模式,但無論我做什麼,我都沒有得到匹配。我想我在做錯模式。此外,有時我得到'未知修飾符'錯誤。下面是主題字符串:preg_match返回未知修飾符

<soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://www.w3.org/2003/05/soap-envelope"> 
<soap:body> 
<sendtransactionsactionresponse xmlns="http://tempuri.org/"> 
<sendtransactionsactionresult>113</sendtransactionsactionresult> 
</sendtransactionsactionresponse> 
</soap:body> 
</soap:envelope> 

我試圖匹配整個<sendtransactionsactionresult>XXX</sendtransactionsactionresult>模式,其中XXX爲3位數字字符。這裏是我迄今爲止嘗試過的模式:

@<sendtransactionsactionresult>[0-9]<\/sendtransactionsactionresult>@i 
/<sendtransactionsactionresult>[0-9]<\/sendtransactionsactionresult>/ 
@(\<[a-z]\>[0-9]\<\/[a-z]\>)@i 
/<[a-z]>[0-9]</[a-z]>/ 
and many many more.. 

沒有一個匹配那..我做錯了什麼?

+1

是否有理由不使用比正則表達式更合適的工具? (Xpath,整個dom擴展名)? – Rangad

+0

好吧,我試圖使用它們,但沒有任何幫助..當我使用'print_r'時,我總是得到一個空對象。 http://stackoverflow.com/questions/32799614/how-to-get-a-value-from-soap-response-using-php – Abhik

回答

1

你缺少你的角色類的量詞。

[0-9]只會匹配一個數字。要匹配一個或多個,您需要使用[0-9]+

@<sendtransactionsactionresult>[0-9]+</sendtransactionsactionresult>@i 
/<sendtransactionsactionresult>[0-9]+<\/sendtransactionsactionresult>/ 
@(<[a-z]+>[0-9]+</[a-z]+>)@i 
/<[a-z]+>[0-9]+<\/[a-z]+>/ 

另外:/只需要如果你也用它作爲分隔符進行轉義。 /...\/.../ vs @.../[email protected]

+0

謝謝,這工作。 – Abhik

相關問題