[[:>:]]或[[:<:]]不匹配

MJN*_*ief 5 regex regex101

我正在尝试[[:>:]]在我的正则表达式中使用,但是在其他角色类例如[[:digit:]]或者[[:word:]]是.出了什么问题?

在线演示

Wik*_*żew 3

这是一个错误,因为PCRE 库本身支持这些构造(起始词边界、[[:<:]]结束[[:>:]]词边界):

COMPATIBILITY FEATURE FOR WORD BOUNDARIES

  In  the POSIX.2 compliant library that was included in 4.4BSD Unix, the
  ugly syntax [[:<:]] and [[:>:]] is used for matching  "start  of  word"
  and "end of word". PCRE treats these items as follows:

    [[:<:]]  is converted to  \b(?=\w)
    [[:>:]]  is converted to  \b(?<=\w)

  Only these exact character sequences are recognized. A sequence such as
  [a[:<:]b] provokes error for an unrecognized  POSIX  class  name.  This
  support  is not compatible with Perl. It is provided to help migrations
  from other environments, and is best not used in any new patterns. Note
  that  \b matches at the start and the end of a word (see "Simple asser-
  tions" above), and in a Perl-style pattern the preceding  or  following
  character  normally  shows  which  is  wanted, without the need for the
  assertions that are used above in order to give exactly the  POSIX  be-
  haviour.
Run Code Online (Sandbox Code Playgroud)

当在 PHP 代码中使用时,它的工作原理是:

if (preg_match_all('/[[:<:]]home[[:>:]]/', 'homeless and home', $m))
{
    print_r($m[0]); 
}
Run Code Online (Sandbox Code Playgroud)

发现Array ( [0] => home). 请参阅在线 PHP 演示

因此,regex101.com 开发团队决定(或忘记)包含对这些配对单词边界的支持

相反,在 regex101.com 上,使用\b所有 4 个 regex101.com 正则表达式引擎(PCRE、JS、Python 和 Go)都支持的单词边界(作为起始和结束边界)。

这些字边界主要由类 POSIX 引擎支持,例如,请参阅此PostgreSQL 正则表达式演示。正则表达式[[:<:]]HR[[:>:]]在 中找到匹配项,但在和Head of HR中找不到匹配项。<A HREF="some.htmlCHROME

其他支持单词边界的正则表达式引擎[[:<:]][[:>:]]基本 R(gsub不带perl=TRUE参数,例如)和 MySQL。

在 Tcl 正则表达式中,有\mfor [[:<:]](起始字边界)和\Mfor 结束字边界([[:>:]])。