简体   繁体   中英

Regular expression tweak

So here is my question

i have the following 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 ?";

i have to extract string inside the double quotes for this i am writting the code

preg_match_all('#"([^"]*)"\s*=\s*"([^"]*)";#', $this->text, $match);

but in the $match variable i am getting the following array

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

)

But the array should be

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

)

Can you tell me the tweak in in my regular expression.

Thanks in advance.

Try this:

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

Note lazy matching .*?

Matching quoted strings that may contain escaped quoted strings is not entirely trivial, but it is not that difficult. Using a non-capturing group, (?:...) , with the OR operator, | , you can specify a list of things to match. You can also repeat the group using + or * .

The \\1 that is used throughout the regex is a reference to the first matched thing. In this case it is the double quote, (") . I have done it like that so it may be easier to change the double quote to something else (a single quote, for instance).

I have commented the regex so it may be a little easier to follow.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM