Q
查找字符串的模式
0
A
回答
1
要明確獲取參數值,您必須首先左邊部分匹配:
/\{myparam/
後面至少一個空間:
/\{myparam\s+/
個
捕捉字符,直到右大括號中找到:
/\{myparam\s+([^}]+)\}/
表達[^}]+
是負字符集,由^
只是左括號後指示;它的意思是「匹配除這些以外的所有字符」。
1
的paramvalue
嘗試用這個表達式:
/\{\w+\s+(\w+)\}/
2
簡單的正則表達式:
/\({[^ ]+?) ([^}]+?)\}/
{[^ ]+?)
- 它會尋找任何東西至少1次發生,但空間並把它放在子模式([^}]+?)
- 它會尋找任何東西至少1次發生,但{
並把它放在子模式。
與preg_match()
功能使用
OR
其他簡單的regex:
preg_match('/([a-z0-9]+?) ([a-z0-9]+?)\}/', $str, $matches);
([a-z0-9]+?)
- AZ 0-9至少一次,不貪([^}]+?)
- az 0-9至少有一次不貪心
輸出:
Array ([0] => myparam paramvalue} [1] => myparam [2] => paramvalue)
1
if(preg_match('/\{'.preg_quote('myparam').' ([^\}]+)\}/', $input, $matches) {
echo "myparam=".$matches[1];
} else {
echo "myparam not found";
}
-
中的preg_match
- , '{' 和 '}' 是特殊字符,所以它們需要進行轉義
- preg_quote可能不是必需的,只要「myparam」永遠不會有特殊正則表達式字符
- (神祕)部分([^}] +)}匹配一個或多個不是'}的字符,後跟'}'
- 匹配成分使第三個arg中的匹配的preg_match,$在這種情況下匹配
0
你可以試試這一個,以及:
.+?\s+([^}]+)
編輯
說明:
.+? means match everything one or more time but its lazy, will prefer to match as less as it can.
\s+ means it will match white-spaces one or more time.
([^}]+) means match everything except `}`(close bracket) one or more time and capture group.
+0
你能否延長你的回答,讓另一位讀者能夠理解你的答案? –
+0
@UwePlonus,我已經用解釋更新了答案。 – NeverHopeless
相關問題
- 1. jQuery查找字符串中的模式
- 2. 查找字符串中的模式linux
- 3. 在字符串C#查找模式
- 4. 使用grep查找字符串模式
- 5. 在長字符串中查找模式?
- 6. Python在字符串中查找模式
- 7. 在字符串中查找模式PHP
- 8. 查找模式並替換爲另一個模式字符串
- 9. 如何在字符串表達式中查找字符串模式Java?
- 10. 一個給定的字符串中查找字符串模式的重複
- 11. 查找字符串中的數字模式並將其刪除
- 12. 查找字符串中的字符串
- 13. 查找字符串中的字符串
- 14. 查找字符串中的字符串
- 15. 正則表達式查找字符串中的字符串中的字符串
- 16. 正則表達式查找字符串中的字符串
- 17. C#正則表達式查找字符串的字符串
- 18. 查找字符串是否符合某種特定模式
- 19. AWK正則表達式查找字符串與模式
- 20. 更好的方法來查找字符串中的模式?
- 21. 模式查找循環中的字符串的問題
- 22. 查找字符串中所有出現的模式的位置
- 23. 如何在一組文件中查找字符串和字符串模式?
- 24. 查找字符串
- 25. 查找字符串
- 26. 查找字符串
- 27. 查找字符串
- 28. 查找字符串
- 29. 查找字符串
- 30. 查找字符串
作品...... Thanks..Can請你告訴什麼是真正做到這一點EXP意思?? –