我有一些極端的無法理解我怎麼只能得到字符的雙引號內時,我只希望它得到字符時,它是這樣的:PHP正則表達式的具體報價
Item.name = "thisCouldBeAnything"
當可以是解析器中的其他雙引號。我很難理解正則表達式是什麼。
我有一些極端的無法理解我怎麼只能得到字符的雙引號內時,我只希望它得到字符時,它是這樣的:PHP正則表達式的具體報價
Item.name = "thisCouldBeAnything"
當可以是解析器中的其他雙引號。我很難理解正則表達式是什麼。
基於您的評論我需要它究竟是Item.name = ""
它不能只是搜索雙引號。假設你想捕捉什麼在雙引號:
preg_match_all('/Item.name = "(.*)"/', $string, $matches);
或者,如果雙引號內的值不應包含雙引號:
preg_match_all('/Item.name = "([^"]+)"/', $string, $matches);
還是捕捉到這一切:
preg_match_all('/(Item.name = ".*")/', $string, $matches);
如果你只需要一個有效的字符串文字,即文字括在雙引號,且該文本可能可能包括反斜槓轉義雙引號,試試這個表達式:
或者(劫持@Dmitry's example):
([\"\'])(.*?(?<!\\))\1
([\"\']) # capture ' or " into group 1
( # second group
.*? # anything lazily
(?<!\\) # neg. lookbehind, make sure there's no backslash
)
\1 # the formerly captured string literal of group 1
注意,你不需要逃避方括號([...]
),但StackOverflow的渲染使得否則它很醜陋的字符串文字。
PHP
片段(注意所謂的「價值」的[不必要]組和雙反斜線轉義):
<?php
$string = <<<DATA
Item.name = "thisCouldBeAnything"
Item.name = "thisCouldBe\"Any\"thing"
Item.name = 'thisCouldBeAnything'
Item.name = 'thisCouldBe\'Any\'"thing'
DATA;
$regex = '~(["\'])(?P<value>.*?(?<!\\\\))\1~';
preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
echo $match["value"] . "\n";
}
?>
嘿這個作品除了下面的數據也作爲Item.value Item.ID等進來,我不好意思在原始帖子上提及; /但我不想像Item.Desc等東西等 –
你的意思是它可能是「」這個「可能」是任何東西「,或者你是指其他地方的雙引號?你需要更多的細節在你的問題,如輸入,應該/不應該匹配等 – AbraCadaver
我的不好,我需要它正是Item.name =「」它不能只搜索雙引號。 –