user11332642
user11332642

Reputation:

Match repeated words

I want to match repeated words like abc abc. Here is the regex:

(\w+)(\s\1)+

This works great for strings like:

pds dd dd dd dd sd

and matches dd dd dd dd as a group and "dd", " dd" but this is not what I am looking for.

I would like to match dd seperately just like a split statement which splits around " " and returns 4 dd. I am not looking for any other methods except a single regex. This can be done using two regex but I am wondering if it is possible to write in a single regex?

Upvotes: 4

Views: 158

Answers (2)

user11332642
user11332642

Reputation:

I ended up using this regex:

(\w+)(?(?=\s+\1\s+\1)|\s+\K(\1))

It does not need an explanation because the regex is much clearly explained at Regex101.

(?something|anotherthing) looks like an if else statement. If something is present then match it else try to find anotherthing.

In above regex, TRUE is null and hence the regex works.

Here is another regex written by revo which does not capture multiple groups:

(?|\b(\w+)(?= +\1\b) +|\G(?!^)(\w+))

Second regex in action

Upvotes: 2

Pupil
Pupil

Reputation: 23948

Convert the string to array by explode() in with space.

Find number of values repeated by array_count_values()

<?php 
$str = 'pds dd dd dd dd sd';
$arr = explode(' ', $str);
$countValues = array_count_values($arr);
if (! empty($countValues)) {
 foreach ($countValues as $countKey => $countValue) {
  if ($countValue > 1) {
   echo "<br/>" . $countKey . ' is repeated ' . $countValue . ' times';
  }
 }
}
echo '<pre>';
print_r($countValues);
echo '</pre>';

Output:

dd is repeated 4 times

Array
(
    [pds] => 1
    [dd] => 4
    [sd] => 1
)

Upvotes: -1

Related Questions