duanqinqiao4844 2013-09-24 17:46
浏览 25
已采纳

PHP正则表达式:如何匹配 和 而不使用[ ]?

I have tested \v (vertical white space) for matching and their combinations, but I found out that \v does not match and . Below is my code that I am using..

$string = "
Test
";

if (preg_match("#\v+#", $string )) {
  echo "Matched";
} else {
  echo "Not Matched";
}

To be more clear, my question is, is there any other alternative to match ?

  • 写回答

7条回答 默认 最新

  • douben8492 2013-09-24 21:50
    关注

    PCRE and newlines

    PCRE has a superfluity of newline related escape sequences and alternatives.

    Well, a nifty escape sequence that you can use here is \R. By default \R will match Unicode newlines sequences, but it can be configured using different alternatives.

    To match any Unicode newline sequence that is in the ASCII range.

    preg_match('~\R~', $string);
    

    This is equivalent to the following group:

    (?>
    |
    ||\f|\x0b|\x85)
    

    To match any Unicode newline sequence; including newline characters outside the ASCII range and both the line separator (U+2028) and paragraph separator (U+2029), you want to turn on the u (unicode) flag.

    preg_match('~\R~u', $string);
    

    The u (unicode) modifier turns on additional functionality of PCRE and Pattern strings are treated as (UTF-8).

    The is equivalent to the following group:

    (?>
    |
    ||\f|\x0b|\x85|\x{2028}|\x{2029})
    

    It is possible to restrict \R to match CR, LF, or CRLF only:

    preg_match('~(*BSR_ANYCRLF)\R~', $string);
    

    The is equivalent to the following group:

    (?>
    |
    |)
    

    Additional

    Five different conventions for indicating line breaks in strings are supported:

    (*CR)        carriage return
    (*LF)        linefeed
    (*CRLF)      carriage return, followed by linefeed
    (*ANYCRLF)   any of the three above
    (*ANY)       all Unicode newline sequences
    

    Note: \R does not have special meaning inside of a character class. Like other unrecognized escape sequences, it is treated as the literal character "R" by default.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度