所以这是我的问题
我有以下字符串
"Are you sure you want to import the MacGourmet data file named \"%@?\"" = "";
"Are you sure you want to import the MacGourmet file named \"%@?\"" = "";
"Are you sure you want to import the MasterCook file named \"%@?\"" = "";
"Are you sure you want to import the Meal-Master file named \"%@?\"" = "";
"Import MasterCook file?" = "Importer le fichier MasterCook ?";
我必须在双引号内提取字符串,因为我正在编写代码
preg_match_all('#"([^"]*)"\s*=\s*"([^"]*)";#', $this->text, $match);
但在$ match变量中我得到以下数组
Array
(
[0] => Array
(
[0] => "" = "";
[1] => "" = "";
[2] => "" = "";
[3] => "" = "";
[4] => "Import MasterCook file?" = "Importer le fichier MasterCook ?";
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => Import MasterCook file?
)
[2] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => Importer le fichier MasterCook ?
)
)
但阵列应该是
Array
(
[0] => Array
(
[0] => "Are you sure you want to import the MacGourmet data file named\"%@?\"" = "";
[1] => "Are you sure you want to import the MacGourmet file named \"%@?\""= "";
[2] => "Are you sure you want to import the MasterCook file named \"%@?\"" = "";
[3] => "Are you sure you want to import the Meal-Master file named \"%@?\"" = "";
[4] => "Import MasterCook file?" = "Importer le fichier MasterCook ?";
)
[1] => Array
(
[0] => Are you sure you want to import the MacGourmet data file named\"%@?\"
[1] => Are you sure you want to import the MacGourmet file named \"%@?\"
[2] => Are you sure you want to import the MasterCook file named \"%@?\"
[3] => Are you sure you want to import the Meal-Master file named \"%@?\"
[4] => Import MasterCook file?
)
[2] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] => Importer le fichier MasterCook ?
)
)
你能告诉我正则表达式中的调整吗。
提前致谢。
答案 0 :(得分:1)
试试这个:
<?php
$string = '
"Are you sure you want to import the MacGourmet data file named \"%@?\"" = "";
"Are you sure you want to import the MacGourmet file named \"%@?\"" = "";
"Are you sure you want to import the MasterCook file named \"%@?\"" = "";
"Are you sure you want to import the Meal-Master file named \"%@?\"" = "";
"Import MasterCook file?" = "Importer le fichier MasterCook ?";
';
preg_match_all('#"(.*?)"\s*=\s*"(.*?)";#', $string, $match);
print_r($match);
注意懒惰匹配.*?
答案 1 :(得分:0)
匹配可能包含转义引用字符串的引用字符串并非完全无关紧要,但并不困难。使用非捕获组(?:...)
和OR运算符|
,您可以指定要匹配的事物列表。您也可以使用+
或*
重复该组。
整个正则表达式中使用的\1
是对第一个匹配的东西的引用。在这种情况下,它是双引号,(")
。我已经这样做了所以将双引号更改为其他内容可能更容易(例如,单引号)。
我评论了正则表达式,因此可能更容易理解。
$examples = array(
'"Are you sure you want to import the MacGourmet data file named\"%@?\"" = ""',
'"Are you sure you want to import the MacGourmet file named \"%@?\""= ""',
'"Are you sure you want to import the MasterCook file named \"%@?\"" = ""',
'"Are you sure you want to import the Meal-Master file named \"%@?\"" = ""',
'"Import MasterCook file?" = "Importer le fichier MasterCook ?"',
);
function myGetValues(array $values) {
static $regex = '/
^ # Start of string
(") # Opening quote; capture for use later
(?P<left> # Left side of equal sign
(?: # Either of these
[^\1]|\\\1 # Not quote or escaped quote
)* # Zero or more times
) # End left side
\1 # Closing quote
\s*=\s* # Equal sign with optional whitespace
\1 # Opening quote
(?P<right> # Right side of equal sign
(?: # Either of these
[^\1]|\\\1 # Not quote or escaped quote
)* # Zero or more times
) # End right side
\1 # Closing quote
$ # End of string
/x';
$results = array();
foreach ($values as $value) {
preg_match($regex, $value, $match);
if ($match) {
// We only need the labelled stuff
$results[] = array(
'left' => $match['left'],
'right' => $match['right'],
);
}
}
return $results;
}
var_dump(myGetValues($examples));