簡體   English   中英

正則表達式調整

[英]Regular expression tweak

所以這是我的問題

我有以下字符串

"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 ?
        )

)

你能告訴我我的正則表達式中的調整嗎?

提前致謝。

嘗試這個:

<?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);

注意延遲匹配.*?

匹配可能包含轉義的引號字符串的引號字符串並不完全是瑣碎的事,但並不是那么困難。 使用非捕獲組(?:...)和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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM