How to create a regex to remove rows with #?

1

I'm starting with regex now so I'm not very good yet with expressions, assuming I have the following variable:

$foo = "
        #01 = linha comentada; 
        02 = valor para ser interpretado; 
        #03 = outra linha comentada;
       ";

How to create a regex to remove lines 01 and 03 leaving only line 02 to be interpreted in the future? Note: Each line ends in ;

    
asked by anonymous 28.04.2017 / 16:53

1 answer

2

Suggestion:

<?php
$foo = "
    #01 = linha comentada; 
    02 = valor para ser interpretado; 
    #03 = outra linha comentada;
";
$limpa = preg_replace('/[\s\t]*#\d+[^;]+;/im', '', $foo);
echo $limpa;

?>

The idea of regex is:

  • % with spaces or tabs, zero or more
  • [\s\t]* the character #
  • # one digit, one or more times
  • \d+ any character excluding [^;]+ one or more times
  • ; the character ;

Example: link

    
28.04.2017 / 17:01