• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 正则表达式简介

    下面描述的是 PCRE 支持的正则表达式语法和语义。正则表达式在 perl 的文档 和另外一些书籍中也有讨论, 其中一些会有丰富的示例。O'Reilly(ISBN 1-56592-257-3) 出版的 Jeffrey Friedl 的《精通正则表达式》一书非常详细的讨论了这些内容。 这里的描述仅作为一个参考手册。

    正则表达式是一个从左到右匹配目标字符串的模式。大多数字符自身就代表一个匹配 它们自身的模式。 作为一个简单的例子,模式 The quick brown fox 匹配目标字符串中与其相同的部分。

    分隔符

    当使用 PCRE 函数的时候,模式需要由分隔符闭合包裹。分隔符可以使任意非字母数字、非反斜线、非空白字符。

    经常使用的分隔符是正斜线(/)、hash符号(#) 以及取反符号(~)。下面的例子都是使用合法分隔符的模式。

    /foo bar/
    #^[^0-9]$#
    +php+
    %[a-zA-Z0-9_-]%
    

    如果分隔符需要在模式内进行匹配,它必须使用反斜线进行转义。如果分隔符经常在模式内出现,一个更好的选择就是是用其他分隔符来提高可读性。

    /http:\/\//
    #http://#
    

    需要将一个字符串放入模式中使用时,可以用 preg_quote() 函数对其进行转义,它的第二个参数(可选)可以用于指定需要被转义的分隔符。

    除了上面提到的分隔符,也可以使用括号样式的分隔符,左括号和右括号分别作为开始和结束 分隔符。

    {this is a pattern}
    

    可以在结束分隔符后面增加模式修饰符。 下面的例子是一个大小写不敏感的匹配:

    #[a-z]#i
    
    Note that bracket style opening and closing delimiters aren't a 100% problem-free solution, as they need to be escaped when they aren't in matching pairs within the expression. That mismatch can happen when they appear inside character classes [...], as most meta-characters lose their special meaning. Consider these examples:
    
    <?php
      preg_match('{[{]}', ''); // Warning: preg_match(): No ending matching delimiter '}'
      preg_match('{[}]}', ''); // Warning: preg_match(): Unknown modifier ']'
      preg_match('{[}{]}', ''); // Warning: preg_match(): Unknown modifier ']'
    ?>
    
    Escaping them solves it:
    
    <?php
      preg_match('{[\{]}', ''); // OK
      preg_match('{[}]}', ''); // OK
      preg_match('{[\}\{]}', ''); // OK
    ?>
    
    preg_match('{[}]}', ''); // Warning: preg_match(): Unknown modifier ']'
    
    preg_match('{[\}]}', ''); // OK
    
    You might also use T-Regx library, which has automatic delimiters:
    
    pattern('Foo(Bar)?')->test($text);
    
    The dirty little delimiter secret they don't tell you ->
    Examples: Balanced delims {\d{2}Some\\{33\\}\w{5}} parses to
    \d{2}Some\\{33\\}\w{5} and {\d{2}Some\{33\}\w{5}} parses to \d{2}Some{33}\w{5}. 
    Un-Balanced delims +\d{2}Some\+33\+\w{5}+ parses to \d{2}Some+33+\w{5} and
    +\d{2}Some\\+33\\+\w{5}+ won't parse because the delimiter is unescaped.
    

    下篇:元字符