我目前正在我的nodejs应用程序中解决这个问题,发现这是,我猜,适合于冒号配对词:
([\w]+:)("(([^"])*)"|'(([^'])*)'|(([^\s])*))
它还匹配引用值。喜欢
a:"b" c:'d e' f:g
ES6中的编码示例:
const regex = /([\w]+:)("(([^"])*)"|'(([^'])*)'|(([^\s])*))/g;
const str = `category:"live casino" gsp:S1aik-UBnl aa:"b" c:'d e' f:g`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
PHP中的示例编码
$re = '/([\w]+:)("(([^"])*)"|\'(([^\'])*)\'|(([^\s])*))/';
$str = 'category:"live casino" gsp:S1aik-UBnl aa:"b" c:\'d e\' f:g';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
可以使用此联机工具检查/测试regex表达式:
https://regex101.com
顺便说一句,如果regex101.com没有删除,您可以浏览该示例编码。
here