Get content that is in parentheses using regex

Asked

Viewed 4,057 times

3

I’ve been studying quite the regex, but I found myself trying to "clear" a text, leaving only the first occurrence, and the data in parentheses, the string is like this:

$string = "Mais de - valor: R$3.21 - Tipo: (+05)";

Like all the text before valor, has a - separating them, I used the explode:

$string2 = explode("-", $string);

I then get the Mais de, wanted to join the (+05) to this result, but in the codes I have tried with preg_match only return me the (05) without the +, remembering that this symbol of more can also be a symbol of less -, guy (-05), someone knows how delimito quotes in regex? I want the end result to be something like:

More than (+05)

Instead of string original.

For those who help me, please comment your code for me try to understand.

  • 1

    Put the final result that you want. I don’t understand what you want to do...

  • Sorry, I updated the question!

3 answers

3


You can use only a regex for that reason: /^([^-]+)[^\(]+(\([^\)]+\))/

In that case you don’t need the blast and you can use only:

$string = "Mais de - valor: R$3.21 - Tipo: (+05)";
preg_match('/^([^-]+)[^\(]+(\([^\)]+\))/', $string, $match);
echo $match[1].$match[2];

Example: https://ideone.com/xMVydh

The regex I used has 2 capture groups.
In the first, [^-]+ accepted n characters that are not - to limit until the - that you currently use for the explode.
In the second, \([^\)]+\), it seeks out parentheses that have to be escaped and accepts n characters that are not ), i.e. until the closure of the parentheses.

  • 1

    I got it! thank you very much Sergio!

2

You can do this without using regex:

$texto = "Mais de - valor: R$3.21 - Tipo: (+05)";

$parte1 = strstr($texto, "-", true);     // Retorna a 1ª ocorrência antes de "-"

$inicio = strpos($texto, "(");           // Encontra a posição da 1ª ocorrência do "("
$fim = strpos($texto, ")", $inicio) + 1; // Encontra a posição da 1ª ocorrência de ")" após "("

$parte2 = substr($texto, $inicio, $fim - $inicio);

echo $parte1 . $parte2 . "\n";           // Mais de (+05)

See demonstração

Use regular expressions only if you really need to.

  • 1

    Using so much strstr and strpos still pays better than ER? Real doubt.

  • 2

    @Papacharlie The documentation of preg_match - section Notes cites: Do not use preg_match() if you only want to check if one string is contained in Another string. Use strpos() or strstr() Instead as they will be Faster. =)

  • @qmechanik very good your solution, I will certainly use it, because I already knew that the strpos() and strstr() are faster. but following the logic of the question, @Sergio’s is better suited, it will help other people who have doubts about ER. but thank you for the reply!

2

If (+05) is always fixed, you can use the last 5 characters.

$str = 'Mais de - valor: R$3.21 - Tipo: (+05)';
echo strstr( $str , ' - ' , true ) . ' ' . substr( $str , -5 );

Output : More than (+05)

Demo

  • 1

    (+05) is not static, but its solution is also good!

Browser other questions tagged

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