简体   繁体   中英

How to do a Regular Expression exclude comma or periods

I'm having issues with the line of PHP. I need to have it select all letters and numbers following the #, but ignore "," or "." (commas or periods). Currently it's including them and I can't seem to get them to exclude them.

Ex: #3431A or #4561AB (but ignore and , or . behind them)

preg_match_all( apply_filters( "wpht_regex_pattern", '/#(\S+)/u' ), strip_tags($content), $hashtags );

You can try "/#[0-9A-Za-z]+/" , if you want to select hashtags only having letters and digits. You may try "/#[^\\s,\\.]+/" , if you want to grab hashtags starting with # and ending just before a whitespace (or tab), comma or period is encountered.

Below is sample PHP code and result:

$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);

Result:

Regex-1:

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

Regex-2:

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

You are matching \\S+ which is 1 or more of any non-whitespace character. In your question, you said you wanted sequences of numners and letters. To get letters and numbers, you need a different pattern.

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);
}

Output:

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

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