Home > Article > Backend Development > How to Make the Dot (.) Match Newlines in Regular Expressions?
Using Regular Expressions to Make Dot Match Newline Characters
In regular expressions, the dot (.) can match any character except for newlines. However, sometimes you might want to match newlines as well.
To achieve this, you can use the DOTALL modifier (/s). It makes the dot match all characters, including newlines.
For example, the following regular expression won't match newlines:
/<div>(.*)</div>/
To make it match newlines, add the DOTALL modifier:
/<div>(.*)</div>/s
However, this might not be ideal if you want a non-greedy match. Instead, you can use a non-greedy match:
/<div>(.*?)</div>/s
Alternatively, if there aren't other tags, you can match everything except '<' characters:
/<div>([^<]*)</div>/
It's also worth noting that you don't necessarily need to use '/' as the delimiter for regular expressions. Using a different character allows you to avoid escaping the slashes in
#<div>([^<]*)</div>#
Nevertheless, it's important to be aware that these solutions may fail in the presence of nested divs, extra whitespace, HTML comments, and other complexities. HTML is quite intricate, so it's often more suitable to use an HTML parser for such tasks.
The above is the detailed content of How to Make the Dot (.) Match Newlines in Regular Expressions?. For more information, please follow other related articles on the PHP Chinese website!