How to mark in bold triple and quadrupled sequential characters of a string?

Asked

Viewed 188 times

8

I want to mark the triple and quadrupled sequences of a string like this:

$string = "AAACDEAAAABBBUUDD";

And I would like to get as a result the following:

<b>AAA</b>CDE<b>AAAA</b><b>BBB</b>UUDD

1 answer

13


PHP version

   $string = 'AAACDEAAAABBBUUDD';
   $bold = preg_replace( '/(.)\1{2,}/', '<b>$0</b>' , $string );

See working on IDEONE


Javascript version

var str = "AAACDEAAAABBBUUDD";
var res = str.replace( /(.)\1{2,}/g, "<b>$&</b>");

See working on IDEONE


Understanding the Regex

  • (.) = any character (switch to (\S) if you don’t want spaces);

  • \1 = the character referred to above by the first group (.);

  • {2,} two or more occurrences of the preceding character. If you only want sequences of three or four characters, disregarding five or more, replace with {2,3} (original plus two equals, or original plus three equals).

That is, with each iterated character, we check if it is followed by 2 more occurrences of the same, totaling the 3 or more desired.

The substitution <b>$0</b> ($& in JS) takes the whole set found, and adds the Bold.

  • 1

    Thanks for the help!

  • only complementing. If there are 3 or more spaces, the replacement will also occur. If in any string this may occur I recommend using ER (.(?! s+)) 1{2,} Testing here https://regex101.com/r/UDabwC/1/ @Pedro I recommend reading http://aurelionet.regex/gui/rearvisor.html

  • 2

    @Marcosxavier is a valid observation, but if he does not want spaces (\S)\1{2,} That’s enough: https://regex101.com/r/UDabwC/2 - anyway, I improved the response based on your comment, thank you.

  • 1

    @Bacco great, I commented to add even.

Browser other questions tagged

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