Home >Backend Development >PHP Tutorial >How to Match \\r and \\n Without Using [\\r\\n]?

How to Match \\r and \\n Without Using [\\r\\n]?

Linda Hamilton
Linda HamiltonOriginal
2024-11-04 07:40:02656browse

How to Match \r and \n Without Using [\r\n]?

Matching r and n without [rn]

When attempting to match the newline characters r and n using v (vertical white space), one may encounter unexpected results. However, there are several alternatives to achieve this match.

Alternatives:

R (Unicode Newline Sequence):

  • By default, R matches Unicode newline sequences in the ASCII range.
  • Using the u (unicode) modifier extends the match to include newline characters outside the ASCII range.
  • For instance:
<code class="php">preg_match('~\R~', $string); // Matches any Unicode newline sequence in ASCII range
preg_match('~\R~u', $string); // Matches any Unicode newline sequence</code>

(*ANYCRLF) R:

  • This variation restricts R to match only CR, LF, or CRLF.
  • For example:
<code class="php">preg_match('~(*BSR_ANYCRLF)\R~', $string); // Matches only CR, LF, or CRLF</code>

The above is the detailed content of How to Match \\r and \\n Without Using [\\r\\n]?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn