Home > Backend Development > PHP Tutorial > PHP regular expression analysis to match multiple lines

PHP regular expression analysis to match multiple lines

高洛峰
Release: 2023-03-05 13:18:01
Original
1386 people have browsed it

PHP matches regular expression analysis of multiple lines. Friends who need it can refer to it. It is mostly used for collection and replacement.

<table> 
<tr><td>aaaa</td></tr> 
<tr><td>bbbb</td></tr> 
<tr><td>cccc</td></tr> 
<tr><td>dddd</td></tr> 
</table>
Copy after login

How to match text like this using PHP regular expressions? ?
My initial thoughts:
Pattern: "/[.\n]*?<\/tr>" (This is wrong)
Ideas: "." can match For any non-newline characters, use the "[.\n]" combination to make it match all characters (including newlines), but there are unforeseen circumstances - tragedy, the result is nothing!
After a lot of gnawing at the bones, I wrote another one
Pattern: "/(.|\n)*?<\/tr>" (This is possible)
Replace "[ ]" is replaced with "()", and "|" is used to make an or selection, and that's it (I'm confused, why can't "[]" also be used as a selection method???)
There is also an answer on the Internet:
PATTEN: "/.*?<\/tr>/is" (very concise and convenient, I agree with this method)

Postscript: Why is it that "[]" is also the selected method? ? ? ? If you know it, don’t forget to tell me... The . in

[] is equivalent to \., please read the regular description carefully.

Generally consider using the pattern modifier when it comes to line breaks. s

s (PCRE_DOTALL)
If this modifier is set, the dot metacharacter in the pattern matches all characters, including newlines. Without this modifier, the dot metacharacter does not match newlines.

[] contains some ranges or combinations
lzTry "/[.]*?<\/tr>/is" and you will know,
Also () Very powerful

For more articles related to regular expression analysis of PHP matching multiple lines, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template