Is it possible to remove the first sequence of the last letter of the regex?

3

I need help with something very specific, which involves good interpretation, which is the following, I need to give a word in%% example% with% the last letter that would be regex to be extended until the first sequence of nt and stop, example ...

in

$txt = "nttttouterttt";

echo preg_replace('/nt/', '', $txt);

saida: tttouterttt

expected result

$txt = "nttttouterttt";

echo preg_replace('/nt/', '', $txt);

saida: outerttt

If the next letter was not 't', do not continue the sequence

$txt = "ntptttouterttt";

echo preg_replace('/nt/', '', $txt);

saida: ptttouterttt

t is removed, but I would like to remove the t's integer, leaving only nt , if the string was nt>ttt< it would only remove nt, is it possible? would there be another method? I accept suggestions and material ...

    
asked by anonymous 30.01.2018 / 19:21

1 answer

5

The Regex used is nt+ or nt* , with the demo on Regex101 .

Where the Regex101 site can be used to test Regexes, much used because of formatting colors, simple design and ease of use.

Explanation

nt+ Validates the part of the String that starts with n and has at least t to infinity

  • n - Corresponds literally to the character n
  • t - Corresponds literally to the character t .
  • + - Quantifier that matches from one to unlimited times, as many times as possible ( greedy ).

nt* Validates the part of the String that starts with n and does not necessarily have t to infinites t

  • n - Corresponds literally to the character n
  • t - Corresponds literally to the character t .
  • * - Quantifier that matches zero to unlimited times, as many times as possible ( greedy ).
31.01.2018 / 13:44