Find term and capture text immediately or after space

Asked

Viewed 68 times

0

I have some payment logs and these are the patterns that they created the file:

Standard 1

Apelido
                          -- isso é um espaço
Fulano de Tal

Standard 2

Apelido:
Fulano de Tal

Standard 3

Apelido: Fulano de Tal

How can I take all the texts, that in the example would be the nickname "So-and-so", always in the next line after the term searched and ignore the lines that have spaces?

2 answers

1

Basically using preg_replace function.

ideone - result of your first example

ideone - result of your second example

ideone - result of your third example

//1- retira os termos indesejados Apelido e :
$patterns = array();
$patterns[0] = '/Apelido/';
$patterns[1] = '/:/';
$replacements = array();
$replacements[1] = '';
$replacements[0] = '';
$str = preg_replace($patterns, $replacements, $str);

//2- substitui quebras de linha (\n), "retornos de carro" (\r) ou tabulações (\t), por um espaço
$str = preg_replace('/[\n\r\t]/', ' ', $str);

//3- remove qualquer espaço em branco duplicado
$str = preg_replace('/\s(?=\s)/', '', $str);

//Retira espaço no ínicio e final
$str = trim($str);

expressions 2 and 3 may be replaced by a single expression

$str = preg_replace(array('/\s{2,}/', '/[\n\r\t]/',), array(" ", " "), $str);

example - ideone

Or joining 1, 2 and 3

$str = preg_replace(array('/Apelido/', '/:/', '/\s{2,}/', '/[\n\r\t]/',), array("",""," ", " "), $str);

example - ideone

The php preg_replace function is a substitution function like str_replace but with some differences, it supports regular expressions and other more powerful features. Preg_replace can be used to make substitutions or even to add specific position characters in a given text.

1

I believe it is simpler to use the function preg_match, why the goal is to get an excerpt from the text and not replace it. Semantic code.

if (preg_match("/(Apelido\:?)(\s+)(.*)/", $test, $matches)) {

    echo $matches[3], PHP_EOL;

}

First, you look for the pattern Apelido[:], being the character : optional, storing this value in group 1. Second, search for the whitespace itself, be it the whitespace, tabulations or line breaks, keeping this value in group 2. Third, search for any content, keeping the value in group 3. Since you need the nickname value, simply access the value of group 3, as done in echo.

See a test:

$tests = [
    "Apelido\n\nFulano de Tal",
    "Apelido:\nFulano de Tal",
    "Apelido: Fulano de Tal"
];

foreach($tests as $test) {

    if (preg_match("/(Apelido\:?)(\s+)(.*)/", $test, $matches)) {

        echo $matches[3], PHP_EOL;

    }

}

The exit will be:

Fulano de Tal
Fulano de Tal
Fulano de Tal

See working on Ideone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.