Home > Article > Backend Development > PHP regular expression analysis to match multiple lines
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!