Regular expression to match a word or its prefix

后端 未结 4 799
盖世英雄少女心
盖世英雄少女心 2020-12-07 13:33

I want to match a regular expression on a whole word.

In the following example I am trying to match s or season but what I have matches

4条回答
  •  余生分开走
    2020-12-07 14:10

    Use this live online example to test your pattern:

    enter image description here

    Above screenshot taken from this live example: https://regex101.com/r/cU5lC2/1

    Matching any whole word on the commandline.

    I'll be using the phpsh interactive shell on Ubuntu 12.10 to demonstrate the PCRE regex engine through the method known as preg_match

    Start phpsh, put some content into a variable, match on word.

    el@apollo:~/foo$ phpsh
    
    php> $content1 = 'badger'
    php> $content2 = '1234'
    php> $content3 = '$%^&'
    
    php> echo preg_match('(\w+)', $content1);
    1
    
    php> echo preg_match('(\w+)', $content2);
    1
    
    php> echo preg_match('(\w+)', $content3);
    0
    

    The preg_match method used the PCRE engine within the PHP language to analyze variables: $content1, $content2 and $content3 with the (\w)+ pattern.

    $content1 and $content2 contain at least one word, $content3 does not.

    Match a specific words on the commandline without word bountaries

    el@apollo:~/foo$ phpsh
    
    php> $gun1 = 'dart gun';
    php> $gun2 = 'fart gun';
    php> $gun3 = 'darty gun';
    php> $gun4 = 'unicorn gun';
    
    php> echo preg_match('(dart|fart)', $gun1);
    1
    
    php> echo preg_match('(dart|fart)', $gun2);
    1
    
    php> echo preg_match('(dart|fart)', $gun3);
    1
    
    php> echo preg_match('(dart|fart)', $gun4);
    0
    

    Variables gun1 and gun2 contain the string dart or fart which is correct, but gun3 contains darty and still matches, that is the problem. So onto the next example.

    Match specific words on the commandline with word boundaries:

    Word Boundaries can be force matched with \b, see:

    Regex Visual Image acquired from http://jex.im/regulex and https://github.com/JexCheng/regulex Example:

    el@apollo:~/foo$ phpsh
    
    php> $gun1 = 'dart gun';
    php> $gun2 = 'fart gun';
    php> $gun3 = 'darty gun';
    php> $gun4 = 'unicorn gun';
    
    php> echo preg_match('(\bdart\b|\bfart\b)', $gun1);
    1
    
    php> echo preg_match('(\bdart\b|\bfart\b)', $gun2);
    1
    
    php> echo preg_match('(\bdart\b|\bfart\b)', $gun3);
    0
    
    php> echo preg_match('(\bdart\b|\bfart\b)', $gun4);
    0
    

    The \b asserts that we have a word boundary, making sure " dart " is matched, but " darty " isn't.

提交回复
热议问题