Question: I have some sequences in a string denoted by "@number" (/@\d/)
I want to remove any redundant sequences, where @2 is followed by @2, 
I only want to remove them if another identical @number sequence is found directly after somewhere in the text, so for `@2lorem@2ipsum` the 2nd @2 is removed, but for `@2lorem@1ipsum@2dolor` nothing is removed because @1 is between the two @2 sequences.

```
"@2randomtext@2randomtext@2randomtext@1bla@2bla2@2bla2"

```

becomes:

```
"@2randomtextrandomtextrandomtext@1bla@2bla2bla2

```

Answer: 
```
"@2randomtext@2randomtext@2randomtext@1bla@2bla2@2bla2".gsub /(?<=(@\d))([^@]*)\1/,'\2'
=> "@2randomtextrandomtextrandomtext@1bla@2bla2bla2"

```
You can split it into tokens:

```
my_string = "@2randomtext@2randomtext@2randomtext@1bla@2bla2@2bla2"
tokens = my_string.scan /(@\d+)?((?:(?!@\d+).)*)/
#=> [["@2", "randomtext"], ["@2", "randomtext"], ["@2", "randomtext"], ["@1", "bla"], ["@2", "bla2"], ["@2", "bla2"]]

```

Then chunk, map and join:

```
tokens.chunk{|x| x[0].to_s}.map{|n, v| [n, v.map(&:last)]}.join
#=> "@2randomtextrandomtextrandomtext@1bla@2bla2bla2"

```