jmoon
jmoon

Reputation: 576

regex parsing / iteration

How would I iterate through around 1,500 text files in a folder, each containing

" Completion rate: 119 ( isComplete: 0 )\r\nFailure rate: 158 HC: 119-158-F "

and get "119" "0" "158" "119-158-F" out? What would be the easiest way to do this? preg_match?

Upvotes: 0

Views: 136

Answers (3)

Joey
Joey

Reputation: 10975

Use preg_match_all() to put each match into an array. Then you can print_r the array or implode it.

// Define regex
$regex = '/[^0-9]*([0-9]+)[^0-9]*([0-9]+)[^0-9]*([0-9]+)[^0-9]*([0-9]+-[0-9]+-[A-Z]+)/s'; 

// Open the directory containing your 1500 files
if ($handle = opendir('/path/folder')) {

    // Loop over each file in the directory
    while (false !== ($entry = readdir($handle))) {

        // Open the file
        $file = file_get_contents($entry);

        // Use preg_match_all to store each value in an array
        preg_match_all($regex, $file, $numbers);

        $numbers = $numbers[0]; // yes you have to do this part

        print_r($numbers); // Or implode instead
        echo '<br />';

    }

    closedir($handle);
}

Upvotes: 1

user895378
user895378

Reputation:

I would use glob to iterate over the text files in the directory like below, though there are other options like opendir and readdir ...

$myDir = '/path/to/text/files';

foreach (glob("$myDir/*.txt") as $filename) {
  $str = file_get_contents($myDir . '/' . $filename);
  $pattern = '/^\s*Completion rate: (\d+) \( isComplete: (\d) \)\s*Failure rate: (\d+) HC: ([A-Z0-9\-]+)\s*$/';
  if (preg_match($pattern, $str, $match)) {
    var_dump($match);
  }
}

Upvotes: 0

Kisaro
Kisaro

Reputation: 281

This works for me just fine using a online regex tester:

    preg_match_all('/[^0-9]*([0-9]+)[^0-9]*([0-9]+)[^0-9]*([0-9]+)[^0-9]*([0-9]+-[0-9]+-[A-Z]+)/s');

It'll get you this:

Array
(
[0] => Array
    (
        [0] => Completion rate: 129 ( isComplete: 0 )\r\nFailure rate: 158 HC: 119-158-F
    )

[1] => Array
    (
        [0] => 129
    )

[2] => Array
    (
        [0] => 0
    )

[3] => Array
    (
        [0] => 158
    )

[4] => Array
    (
        [0] => 119-158-F
    )

)

Upvotes: 0

Related Questions