Regex to extract content from string

3

Good evening guys,

Thank you for visiting and suggesting to help me. I am very bad at regex so I come to ask for your help.

I have the following string which can also vary by example:

string(49) "02/12/2018 (Assessment 2) = /86= | Weight: 50.00%"
string(49) "02/12/2018 (Assessment 2) = 50.83/86= | Weight: 50.00%"

In the first case even without the value I need to collect 00.00.

I need to extract to an array as follows:

$dados[ "date" ] = "02/12/2018"
$dados[ "markOK" ] = "50"
$dados[ "markTotal" ] = "86"
$dados[ "weight" ] = "50.00"

Other output examples:

string(49) "02/12/2018 (Assessment 2) = /86= | Weight: 50.00%"
string(59) "06/11/2018 (Assessment 2) = 22.40/35=32.00 | Weight: 50.00%"
string(49) "04/12/2018 (Assessment 2) = /60= | Weight: 50.00%"
string(59) "11/09/2018 (Assessment 2) = 27.00/40=33.75 | Weight: 50.00%"
string(59) "09/09/2018 (Assessment 2) = 30.00/30=50.00 | Weight: 50.00%"
string(59) "14/08/2018 (Assessment 2) = 31.00/40=38.75 | Weight: 50.00%"
string(59) "19/06/2018 (Assessment 2) = 63.00/72=43.75 | Weight: 50.00%"
string(59) "17/06/2018 (Assessment 2) = 45.00/45=50.00 | Weight: 50.00%"
string(59) "22/05/2018 (Assessment 2) = 11.00/55=10.00 | Weight: 50.00%"
    
asked by anonymous 26.11.2018 / 00:10

2 answers

1

I would not use regex for this. You can break the string into array by space and make a forEach by associating the values:

<?
$string = "02/12/2018 (Assessment 2) = /86= | Weight: 50.00%";
$array = explode(" ", $string);

forEach($array as $item){
   // verifica quantas barras "/" a string possui
   preg_match_all("~\/~", $item, $matches);

   // se tiver 2 barras é uma data
   if(sizeof($matches[0]) == 2){
      $dados[ "date" ] = $item;
   }

   if(sizeof($matches[0]) == 1){
      $mark = explode("/", $item);
      // se o primerio estiver vazio, retorna 00.00
      $dados[ "markOK" ] = $mark[0] ? $mark[0] : "00.00";
      $dados[ "markTotal" ] = $mark[1];
   }

   if(strrpos($item, "%")){
      $dados[ "weight" ] = str_replace("%", "", $item);
   }
}

var_dump($dados);
?>

The result is:

array(4) {
  ["date"] => "02/12/2018"
  ["markOK"] => "00.00"
  ["markTotal"] => "86="
  ["weight"]=> "50.00"
}

IDEONE

    
26.11.2018 / 02:06
0

In your case you will need to use groups in regex to delimit and also identify what you want to look for / extract, groups are identified by the use of elements in parentheses.

This program uses the samples you put in the question as the base and how the regular expression got (rather) long I separated into smaller pieces to make it easier to read and understand (as well as to adjust):

<?php

    $sampleData = array(
        "02/12/2018 (Assessment 2) = /86= | Weight: 50.00%",
        "06/11/2018 (Assessment 2) = 22.40/35=32.00 | Weight: 50.00%",
        "04/12/2018 (Assessment 2) = /60= | Weight: 50.00%",
        "11/09/2018 (Assessment 2) = 27.00/40=33.75 | Weight: 50.00%",
        "09/09/2018 (Assessment 2) = 30.00/30=50.00 | Weight: 50.00%",
        "14/08/2018 (Assessment 2) = 31.00/40=38.75 | Weight: 50.00%",
        "19/06/2018 (Assessment 2) = 63.00/72=43.75 | Weight: 50.00%",
        "17/06/2018 (Assessment 2) = 45.00/45=50.00 | Weight: 50.00%",
        "22/05/2018 (Assessment 2) = 11.00/55=10.00 | Weight: 50.00%"
    );

    $regex = '/$'.                          // começo da linha
        '([0-9]{2}\/[0-9]{2}\/[0-9]{4})'.   // 99/99/9999 (obrigatório) [1]
        ' \(Assessment.+\)'.                // " (Assessment *) " -- ignorado
        ' = '.                              // " = " -- ignorado
        '([0-9.]{5})?'.                     // 99.99 (opcional) [2]
        '\/'.                               // "/" -- ignorado
        '([0-9]{2})?'.                      // 99 (opcional) [3]
        '='.                                // "=" -- ignorado                  
        '([0-9\.]{5})?'.                    // 99.99 (opcional) [4]
        '.+Weight: '.                       // "*Weight: " -- ignorado
        '([0-9\.]{5})'.                     // 99.99 (obrigatório) [5]
        '%$/';                              // "%" e termina a linha;

    foreach ($sampleData as $i){
        preg_match($regex, $i, $matchList);
        print_r($matchList);
    }

?>

At the end, the loop foreach() at the end takes the array item, applies regex, and puts the result of the operation on the array in> $matchList .

Here [is a sample of the result:

Array
(
    [0] => 04/12/2018 (Assessment 2) = /60= | Weight: 50.00%
    [1] => 04/12/2018
    [2] => 
    [3] => 60
    [4] => 
    [5] => 50.00
)

The first item is always what was used in the query while the others are what was found and if it is not, it will be empty.

    
26.11.2018 / 02:03