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


i have tested \v (vertical white space) matching \r\n , combinations, found out \v not match \r , \n. below code using..

$string = " test ";  if (preg_match("#\v+#", $string )) {   echo "matched"; } else {   echo "not matched"; } 

to more clear, question is, there other alternative match \r\n?

pcre , newlines

pcre has superfluity of newline related escape sequences , alternatives.

well, nifty escape sequence can use here \r. default \r match unicode newlines sequences, can configured using different alternatives.

to match unicode newline sequence in ascii range.

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

this equivalent following group:

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

to match unicode newline sequence; including newline characters outside ascii range , both line separator (u+2028) , paragraph separator (u+2029), want turn on u (unicode) flag.

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

the u (unicode) modifier turns on additional functionality of pcre , pattern strings treated (utf-8).

the equivalent following group:

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

it possible restrict \r match cr, lf, or crlf only:

preg_match('~(*bsr_anycrlf)\r~', $string); 

the equivalent following group:

(?>\r\n|\n|\r) 

additional

five different conventions indicating line breaks in strings supported:

(*cr)        carriage return (*lf)        linefeed (*crlf)      carriage return, followed linefeed (*anycrlf)   of 3 above (*any)       unicode newline sequences 

note: \r not have special meaning inside of character class. other unrecognized escape sequences, treated literal character "r" default.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -