简体   繁体   中英

extracting string with preg_match

I have strings containing dash - characters, I want to extract the portion of the string before the first dash character is encountered except in the case where the dash is in single/double quotes.

ie.

Theory 'Gabe B - Tailor' Jacket - nordstrom.com I want to extract Theory 'Gabe B - Tailor' Jacket

Theory "Gabe B - Tailor" Jacket - nordstrom.com I want to extract Theory "Gabe B - Tailor" Jacket

Tailor Jacket - Jackets - nordstrom.com I want to extract Tailor Jacket

What regex can I use with preg_match to achieve the result?

You could use an expression like this to handle single and double quoting (without escapes):

(?:[^-]+|"[^"]*"|'[^']*')+

Or just capture everything till the last - :

(.+)-

How about a non-regex alternative?

$input = "'Gabe B - Tailor' Jacket - nordstrom.com";

$insideQuotes = false;
for ($i=0 ; $i<strlen($input) ; $i++) {

    if (!$insideQuotes && $input[$i] == "-") {
        break;
    }

    if ($input[$i] == "'" || $input[$i] == '"') {
        $insideQuotes = !$insideQuotes;
        continue;
    }
}

echo substr($input, 0, $i);

我相信您正在寻找此正则表达式-

([^-"']|"[^"]*"|'[^']*')*?(?=\s*\-)

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