What are the potential pitfalls when using regular expressions (regex) in PHP to extract data from text files?

Potential pitfalls when using regular expressions in PHP to extract data from text files include: 1. Greedy matching: Regular expressions can be greedy by default, meaning they may match more text than intended. This can lead to incorrect extraction of data. 2. Lack of error handling: If the regex pattern is not well-formed or does not match the text file correctly, it can result in errors or unexpected behavior. 3. Performance issues: Complex regex patterns can be computationally expensive and slow down the extraction process. To address these pitfalls, it is important to test regex patterns thoroughly, use non-greedy matching where appropriate, and implement error handling to handle any issues that may arise.

<?php

// Sample text file content
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

// Regex pattern to extract words between "Lorem" and "sit"
$pattern = '/Lorem(.*?)sit/';

// Perform the regex match
if (preg_match($pattern, $text, $matches)) {
    // Extracted data
    $extractedData = $matches[1];
    echo $extractedData;
} else {
    echo "No match found.";
}