$string = "Hello world! The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested."; 

It is necessary to find words with 2 or more identical letters (Hello, standart, ...). Can this be done through regular expressions? And what are some ways you can do it?

  • Split by spaces, delete non-alphabetic characters. Then in each word check that the next letter is equal to the previous one. Like so. - Vladimir Martyanov
  • Cycle through the word and check. - Vladimir Martyanov
  • Specify the question - do you mean the same letters in general, or the same letters that follow in succession? - Alexey Shatrov
  • Generally the same letters. - Vuland
  • Then the regulars are unlikely - Alexey Shatrov

2 answers 2

Yes, it is possible through regular expressions, like this:

 \w*(\w)(?=\w*\1)\w* 

Test at regex101.com

If necessary, replace \w with another class of characters (for example, if numbers are not needed).

Decryption:

 \w* # НСкотороС количСство символов (\w) # Π‘Ρ€Π΅Π΄ΠΈ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Π΅ΡΡ‚ΡŒ Ρ‚Π°ΠΊΠΎΠΉ (?=\w* # ПослС ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ Π΅Ρ‰Π΅ Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ символы \1) # ΠΈ Ρ‚ΠΎΡ‡Π½ΠΎ Ρ‚Π°ΠΊΠΎΠΉ ΠΆΠ΅ (ссылка Π½Π°Π·Π°Π΄) \w* # ΠΈ ΠΏΠΎΡ‚ΠΎΠΌ ΠΌΠΎΠ³ΡƒΡ‚ Π±Ρ‹Ρ‚ΡŒ Π΅Ρ‰Π΅ ΠΊΠ°ΠΊΠΈΠ΅ Ρ‚ΠΎ символы слова 
  • Thank you so much, now I am picking up with a regular season. Then I'll try again through the cycle. - Vuland
  • That's for sure - learn! - Alexey Shatrov
  • @Mike probably, at the beginning and end of \ b it doesn’t hurt to capture the whole word - splash58
  • one
    @Mike habitually decides everything in sqlfiddle, even regulars: D - teran
  • @teran :) habit is terrible power - Mike

Here's a point of reference for you to solve through cycles.

 $string = "Hello world! The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested."; $words=explode(" ",$string); foreach ($words as $word) { $letters=str_split($word); $letters=array_unique($letters); if (count($letters)!=strlen($word)) { $result[]=$word; } } print_r($result); Array ( [0] => Hello [1] => standard [2] => 1500s [3] => reproduced [4] => interested. ) 

This is not a completely ready-made solution, you still need to clear commas, etc., and decide what to do with the numbers, delete them or leave.

Demo here