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

PHP regular expression analysis to match multiple lines

高洛峰
高洛峰Original
2017-02-06 16:35:421405browse

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>

How to match text like this using PHP regular expressions? ?
My initial thoughts:
Pattern: "/a34de1251f0d9fe1e645927f19a896e8[.\n]*?52a7d8ce3ad17982f64ae622fb285c16" (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: "/a34de1251f0d9fe1e645927f19a896e8(.|\n)*?52a7d8ce3ad17982f64ae622fb285c16" (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: "/a34de1251f0d9fe1e645927f19a896e8.*?52a7d8ce3ad17982f64ae622fb285c16/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 "/a34de1251f0d9fe1e645927f19a896e8[.]*?52a7d8ce3ad17982f64ae622fb285c16/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!

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