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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值