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

/($word)/i

问题是,如果我使用 /(Foo)/i 然后像这样的话 Food 得到匹配。单词两侧需要有空格或单词边界。

如何修改我的表达式以仅匹配该单词 Foo 当它是句首、句中或句尾的单词时?

有帮助吗?

解决方案

使用字边界:

/\b($word)\b/i

或者,如果你正在寻找“S.P.E.C.T.R.E.”像思南Ünür的示例:

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

其他提示

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

假设您正在使用 PCRE 或类似的东西:

enter image description here

上面的屏幕截图取自这个实例: http://regex101.com/r/cU5lC2

将命令行上的任何整个单词与 (\w+)

我将使用 phpsh 交互式 shell乌班图12.10 来证明 PCRE 正则表达式引擎 通过称为 预匹配

启动phpsh,将一些内容放入变量中,匹配单词。

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。枪4没有。然而,寻找单词可能是一个问题 fart 火柴 farty. 。要解决此问题,请在正则表达式中强制执行单词边界。

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

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

所以它与前面的例子相同,除了这个词 fart 与一个 \b 内容中不存在单词边界: farty.

使用\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"

使用字边界\ B,

以下(使用四个逃逸)的作品在我的环境:Mac上,Safari浏览器版本10.0.3(12602.4.8)

var myReg = new RegExp(‘\\\\b’+ variable + ‘\\\\b’, ‘g’)

如果你在记事本中做++

[\w]+ 

会给你整个单词,你可以添加括号把它作为一个群体。例如:conv1 = Conv2D(64, (3, 3), activation=LeakyReLU(alpha=a), padding='valid', kernel_initializer='he_normal')(inputs)。我想LeakyReLU移动到自己的行作为注释,并取代当前激活。在记事本++这可以通过使用遵循找到命令进行:

([\w]+)( = .+)(LeakyReLU.alpha=a.)(.+)

和替换命令变为:

\1\2'relu'\4 \n    # \1 = LeakyReLU\(alpha=a\)\(\1\)

在空间是保持正确的格式在我的代码。 :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top