我正在尝试制作一个正则表达式,它将从Ruby和PHP等语言中捕获一个有效的,任意的字符串(就像你可能输入的那样),例如:
"lol" // valid
'was' // valid
"\"say\"" // valid
'\'what\'' // valid
"m"y" // invalid
'ba'd' // invalid
"th\\"is" // invalid
'su\\'cks' // invalid
我有点卡住尝试正确匹配内容中的转义引号,同时失败双重转义然后引用。
任何帮助表示赞赏!
答案 0 :(得分:4)
这匹配前4行并拒绝最后4行:
^(["'])(\\.|(?!\\|\1).)*\1$
快速解释:
^ # the start of the input
(["']) # match a single- or double quote and store it in group 1
( # open group 2
\\. # a backslash followed by any char
| # OR
(?!\\|\1). # if no backslash or the quote matched in group 1 can be seen ahead, match any char
)* # close group 2 and repeat it zero or more times
\1 # the same quote as matched in group 1
$ # the end of the input
这是一个小小的PHP演示:
<?php
$tests = array(
'"lol"',
"'was'",
'"\\"say\\""',
"'\\'what\\''",
'"m"y"',
"'ba'd'",
'"th\\\\"is"',
"'su\\\\'cks'"
);
foreach($tests as $test) {
if(preg_match('/^(["\'])(\\\\.|(?!\\\\|\1).)*\1$/', $test)) {
echo "valid : " . $test . "\n";
}
else {
echo "invalid : " . $test . "\n";
}
}
?>
产生:
valid : "lol"
valid : 'was'
valid : "\"say\""
valid : '\'what\''
invalid : "m"y"
invalid : 'ba'd'
invalid : "th\\"is"
invalid : 'su\\'cks'
可以在ideone上看到:http://ideone.com/60mtE