如何使用正则表达式排除逗号或句点

时间:2017-08-24 22:24:34

标签: php html

我遇到了PHP系列的问题。我需要选择#后的所有字母和数字,但忽略","或"。" (逗号或句号)。目前它包括它们,我似乎无法将它们排除在外。

Ex:#3431A或#4561AB(但忽略和,或者在他们后面)

[^\[]\d+[^\]]

2 个答案:

答案 0 :(得分:1)

如果您想选择仅包含字母和数字的主题标签,可以尝试"/#[0-9A-Za-z]+/"。 您可以尝试"/#[^\s,\.]+/",如果您想抓取以#开头并在遇到空白(或制表符),逗号或句号之前结束的主题标签。

下面是示例PHP代码和结果:

$content="I need to have it select all letters and numbers following the #, but ignore ',' or '.' (commas or periods). Ex: #3431A   D, #3431AB or #4561AB.";

echo "<h2>Regex-1:</h2>";
preg_match_all( "/#[0-9A-Za-z]+/", $content, $hashtags );
print_r($hashtags);

echo "<h2>Regex-2:</h2>";
preg_match_all( "/#[^\s,\.]+/", $content, $hashtags );
print_r($hashtags);

结果:

Regex-1:

Array ( [0] => Array ( [0] => #3431A [1] => #3431AB [2] => #4561AB ) )

Regex-2:

Array ( [0] => Array ( [0] => #3431A [1] => #3431AB [2] => #4561AB ) )

答案 1 :(得分:0)

您匹配的\S+是任何非空白字符中的一个或多个。在你的问题中,你说你想要数字和字母序列。要获得字母和数字,您需要一个不同的模式。

function testFilter($test) {
    $content = $test['test'];
    echo "Testing {$content}\n";
    preg_match_all( apply_filters( "wpht_regex_pattern", '/#([A-Za-z0-9]+)/u' ), strip_tags($content), $hashtags );

    $expect = $test['expect'];
    echo "    ";
    if ( ! empty($expect) ) {
        $tmp = implode(',', $hashtags[1]);
        if ( $tmp != $expect ) echo "FAIL ";
        else echo "PASS ";
    }
    else {
        echo "     ";
    }
    echo 'Hashtags: '. implode(',', $hashtags[1]);
    echo PHP_EOL;
}        

$contentTest = [
    ['test' => '#shoes, #friends, #beach',      'expect' => 'shoes,friends,beach'],
    ['test' => '#shoes, #friends6, #2beach',    'expect' => 'shoes,friends6,2beach'],
    ['test' => '#shoes, #frie_nds, #be^ach',    'expect' => 'shoes,frie,be'],
    ['test' => 'blah blah #shoes, #friends, #beach', 'expect' => 'shoes,friends,beach'],
    ['test' => '#shoes, #friends, #beach,',     'expect' => 'shoes,friends,beach'],
    ['test' => '#shoes, #friends, #beach,#',    'expect' => 'shoes,friends,beach'],
    ['test' => '#shoes, #friends, #beach som trailing text', 'expect' => 'shoes,friends,beach'],
    ['test' => '#3431A, #345ADF', 'expect' => '3431A,345ADF'],
    ['test' => 'The quick brown #fox gave the #99dogs codes #A00BZ90A #45678blah #0569509 #09XX09', 'expect' => 'fox,99dogs,A00BZ90A,45678blah,0569509,09XX09'],
];
foreach ($contentTest as $t) {
    testFilter($t);
}

输出:

Testing #shoes, #friends, #beach
    PASS Hashtags: shoes,friends,beach
Testing #shoes, #friends6, #2beach
    PASS Hashtags: shoes,friends6,2beach
Testing #shoes, #frie_nds, #be^ach
    PASS Hashtags: shoes,frie,be
Testing blah blah #shoes, #friends, #beach
    PASS Hashtags: shoes,friends,beach
Testing #shoes, #friends, #beach,
    PASS Hashtags: shoes,friends,beach
Testing #shoes, #friends, #beach,#
    PASS Hashtags: shoes,friends,beach
Testing #shoes, #friends, #beach som trailing text
    PASS Hashtags: shoes,friends,beach
Testing #3431A, #345ADF
    PASS Hashtags: 3431A,345ADF
Testing The quick brown #fox gave the #99dogs codes #A00BZ90A #45678blah #0569509 #09XX09
    PASS Hashtags: fox,99dogs,A00BZ90A,45678blah,0569509,09XX09