正则表达式只匹配整个单词

2024-05-17 03:43:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个regex表达式,用于查找给定内容块中的所有单词,不区分大小写,这些内容包含在存储在数据库中的词汇表中。这是我的模式:

/($word)/i

问题是,如果我使用/(Foo)/i,那么像Food这样的单词将得到匹配。单词的两边必须有空格或单词边界。

如何修改表达式,使其仅与单词Foo匹配,而该单词位于句子的开头、中间或结尾?


Tags: 词汇表数据库内容foofood表达式结尾模式
3条回答

使用单词边界:

/\b($word)\b/i

或者如果你在搜索“S.p.E.C.T.R.E.”,就像在西南的例子中一样:

/(?:\W|^)(\Q$word\E)(?:\W|$)/i

使用\b可以产生令人惊讶的结果。你最好弄清楚是什么把一个词从它的定义中分离出来,并把这些信息整合到你的模式中。

#!/usr/bin/perl

use strict; use warnings;

use re 'debug';

my $str = 'S.P.E.C.T.R.E. (Special Executive for Counter-intelligence,
Terrorism, Revenge and Extortion) is a fictional global terrorist
organisation';

my $word = 'S.P.E.C.T.R.E.';

if ( $str =~ /\b(\Q$word\E)\b/ ) {
    print $1, "\n";
}

输出:

Compiling REx "\b(S\.P\.E\.C\.T\.R\.E\.)\b"
Final program:
   1: BOUND (2)
   2: OPEN1 (4)
   4:   EXACT  (9)
   9: CLOSE1 (11)
  11: BOUND (12)
  12: END (0)
anchored "S.P.E.C.T.R.E." at 0 (checking anchored) stclass BOUND minlen 14
Guessing start of match in sv for REx "\b(S\.P\.E\.C\.T\.R\.E\.)\b" against "S.P
.E.C.T.R.E. (Special Executive for Counter-intelligence,"...
Found anchored substr "S.P.E.C.T.R.E." at offset 0...
start_shift: 0 check_at: 0 s: 0 endpos: 1
Does not contradict STCLASS...
Guessed: match at offset 0
Matching REx "\b(S\.P\.E\.C\.T\.R\.E\.)\b" against "S.P.E.C.T.R.E. (Special Exec
utive for Counter-intelligence,"...
   0           |  1:BOUND(2)
   0           |  2:OPEN1(4)
   0           |  4:EXACT (9)
  14      |  9:CLOSE1(11)
  14      | 11:BOUND(12)
                                  failed...
Match failed
Freeing REx: "\b(S\.P\.E\.C\.T\.R\.E\.)\b"

要匹配任何一个单词,您可以使用模式(\w+)

假设您正在使用PCRE或类似的工具:

enter image description here

以上截图摘自本例:http://regex101.com/r/cU5lC2

(\w+)匹配命令行中的所有单词

我将使用Ubuntu 12.10上的phpsh interactive shell通过名为preg_match的方法演示PCRE regex engine

启动phpsh,将一些内容放入变量中,匹配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

preg_match方法使用PHP语言中的PCRE引擎来分析变量:$content1$content2$content3(\w)+模式。

$content1和$content2至少包含一个单词,$content3不包含。

(dart|fart)匹配命令行中的许多文字单词

el@apollo:~/foo$ phpsh

php> $gun1 = 'dart gun';
php> $gun2 = 'fart gun';
php> $gun3 = 'farty 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

变量gun1和gun2包含字符串dart或fart。gun4没有。然而,查找单词fart匹配farty可能是一个问题。要解决此问题,请在regex中强制使用单词边界。

将命令行中的文字单词与单词边界匹配

el@apollo:~/foo$ phpsh

php> $gun1 = 'dart gun';
php> $gun2 = 'fart gun';
php> $gun3 = 'farty 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

所以它和前面的例子一样,只是内容中不存在单词边界为\b的单词fart

相关问题 更多 >