How can regular expressions (Regex) be used to search for specific content in HTML?

Regular expressions can be used to search for specific content in HTML by defining patterns that match the desired content. This can be useful for extracting specific information from HTML documents, such as finding all instances of a certain tag or attribute. In PHP, you can use functions like preg_match() or preg_match_all() to apply regular expressions to HTML strings and extract the desired content.

$html = '<div><p>Hello, this is some sample HTML content.</p></div>';
$pattern = '/<p>(.*?)<\/p>/'; // Regex pattern to match content within <p> tags

if (preg_match($pattern, $html, $matches)) {
    echo "Found content within <p> tags: " . $matches[1];
} else {
    echo "No content found within <p> tags.";
}