按点分割字符串,除非点位于双引号内

发布于 2024-10-24 05:36:11 字数 355 浏览 5 评论 0原文

我讨厌正则表达式,但对于我正在做的事情,我确信没有其他更简单的选择。 无论如何,我一直在使用这个表达式:

/(([a-zA-z_]+)[\.]?+)+/

尝试匹配与此类似的内容

"text.lol".something.another etc..

并且使用 preg_match 返回一个类似于的数组

Array
(
    [0] => "text.lol"
    [1] => something
    [2] => another
)

但是我得到的只是数组中第一个匹配项两次?

I hate regular expressions, but for what I am doing I'm sure there is no other simpler option.
Anyway I have been working with this experssion:

/(([a-zA-z_]+)[\.]?+)+/

To try and match something similar to this

"text.lol".something.another etc..

And with preg_match return an array similar to

Array
(
    [0] => "text.lol"
    [1] => something
    [2] => another
)

But instead all I am getting is the first matched item twice in the array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2024-10-31 05:36:11

这给出了您指定的输入所需的输出:

$s = '"text.lol".something.another';
preg_match_all('/"[^"]+"|[^.]+/', $s, $m);
$values = $m[0];
print_r($values);

这是允许转义引号的完整实现:

function encode($original) {
    foreach ($original as &$s) {
        $s = addslashes($s);
        if (strpos($s, '.') !== false) $s = '"'.$s.'"';
    }
    return join('.', $original);
}

function decode($s) {
    // These regular expressions courtesy of ridgerunner:
    preg_match_all('/"([^"\\\\]*+(?:\\\\.[^"\\\\]*)*)"|([^.]+)/', $s, $m);
    // This one has poorer performance, but is easier to read:
    // preg_match_all('/"((?:\\\\.|[^"\\\\])+)"|([^.]+)/', $s, $m);
    $values = array();
    foreach ($m[1] as $k => $v) $values[] = stripslashes($v? $v : $m[2][$k]);
    return $values;
}

$test_cases = array('a.b', 'a\\', '.a\\', 'a.b"c', '"a');
$encoded = encode($test_cases);
$decoded = decode($encoded);

echo '<pre>Encoded: '.$encoded."\n";
echo print_r($decoded, 1).'</pre>';

​​ 输出:

Encoded: "a.b".a\\.".a\\"."a.b\"c".\"a
Array
(
    [0] => a.b
    [1] => a\
    [2] => .a\
    [3] => a.b"c
    [4] => "a
)

This gives the output you want for the input you specified:

$s = '"text.lol".something.another';
preg_match_all('/"[^"]+"|[^.]+/', $s, $m);
$values = $m[0];
print_r($values);

Here's a full implementation that allows escaped quotes:

function encode($original) {
    foreach ($original as &$s) {
        $s = addslashes($s);
        if (strpos($s, '.') !== false) $s = '"'.$s.'"';
    }
    return join('.', $original);
}

function decode($s) {
    // These regular expressions courtesy of ridgerunner:
    preg_match_all('/"([^"\\\\]*+(?:\\\\.[^"\\\\]*)*)"|([^.]+)/', $s, $m);
    // This one has poorer performance, but is easier to read:
    // preg_match_all('/"((?:\\\\.|[^"\\\\])+)"|([^.]+)/', $s, $m);
    $values = array();
    foreach ($m[1] as $k => $v) $values[] = stripslashes($v? $v : $m[2][$k]);
    return $values;
}

$test_cases = array('a.b', 'a\\', '.a\\', 'a.b"c', '"a');
$encoded = encode($test_cases);
$decoded = decode($encoded);

echo '<pre>Encoded: '.$encoded."\n";
echo print_r($decoded, 1).'</pre>';

Output:

Encoded: "a.b".a\\.".a\\"."a.b\"c".\"a
Array
(
    [0] => a.b
    [1] => a\
    [2] => .a\
    [3] => a.b"c
    [4] => "a
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文