Search code examples
phpregexsumtext-extraction

Sum the last number at the end of qualifying lines in a .txt file


I have this a file named "test.txt" which has the following content:

BEGIN_SESSION 7
1h+ 47
30mn-1h 20
15mn-30mn 16
5mn-15mn 43
2mn-5mn 29
30s-2mn 35
0s-30s 170
END_SESSION

I would like a function that extracts its data and for the numbers after "30s-2mn" and "0s-30s" to be summed ("35+170=205").

I've tried this but it doesn't seem to work:

preg_match("/BEGIN_SESSION(.*)END_SESSION/is", $test, $matches);
$session = $matches[0] ;
preg_match('/^30s-2mn ([0-9]{14}) /ms', $test, $matches); 
$data = $matches;
var_dump($data);

What's the problem in my code?


Solution

  • First there's a mistake in your PHP, for your second preg_matchyou again use $test as subject instead of $session.

    Second your RegEx simply can't match. The caret (^) tells PHP to start the search at the beginning of the test subject but there's no 30s..., this 30s... is somewhere in the test string. So remove that. And then you tell PHP there are 14 numbers from 0-9, but you are just looking for two. So a working RegEx would be:

    /30s-2mn ([0-9]{2})/ms
    

    Corrected:

    preg_match("/BEGIN_SESSION(.*)END_SESSION/is", $test, $matches);
    $session = $matches[0] ;
    preg_match('/30s-2mn ([0-9]{2})/ms', $session, $matches); 
    $data= $matches;
    var_dump($data);
    

    What prints out:

    array(2) { [0]=> string(10) "30s-2mn 35" [1]=> string(2) "35" }
    

    And now do some maths:

    preg_match("/BEGIN_SESSION(.*)END_SESSION/is", $test, $matches);
    $session = $matches[0];
    preg_match('/30s-2mn ([0-9]{2})/ms', $session, $matches); 
    $a = $matches[1];
    preg_match('/0s-30s ([0-9]{3})/ms', $session, $matches); 
    $b = $matches[1];
    
    var_dump($a+$b);
    

    prints:

     int(205)