regex: find one-digit number

前端 未结 4 2059
温柔的废话
温柔的废话 2020-12-10 13:24

I need to find the text of all the one-digit number.

My code:

$string = \'text 4 78 text 558 my.name@gmail.com 5 text 78998 text\';
$pattern = \'/ [\         


        
4条回答
  •  我在风中等你
    2020-12-10 14:13

    It really depends on where the numbers can appear and whether you care if they are adjacent to other characters (like . at the end of a sentence). At the very least, I would use word boundaries so that you can get numbers at the beginning and end of the input string:

    $pattern = '/\b\d\b/';
    

    But you might consider punctuation at the end like:

    $pattern = '/\b\d(\b|\.|\?|\!)/';
    

提交回复
热议问题