Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

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

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 ?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...