PHP Regex: How to match \r and \n without using [\r\n]?

后端 未结 7 1113
予麋鹿
予麋鹿 2020-11-27 05:58

I have tested \\v (vertical white space) for matching \\r\\n and their combinations, but I found out that \\v does not match \\r

7条回答
  •  春和景丽
    2020-11-27 06:45

    This doesn't answer the question for alternatives, because \v works perfectly well

    \v matches any character considered vertical whitespace; this includes the platform's carriage return and line feed characters (newline) plus several other characters, all listed in the table below.

    You only need to change "#\v+#" to either

    • "#\\v+#" escape the backslash

    or

    • '#\v+#' use single quotes

    In both cases, you will get a match for any combination of \r and \n.

    Update:

    Just to make the scope of \v clear in comparison to \R, from perlrebackslash

    • \R
      \R matches a generic newline; that is, anything considered a linebreak sequence by Unicode. This includes all characters matched by \v (vertical whitespace), ...

提交回复
热议问题