I'm trying to extract all posible combinations of 3 letters from a String following the pattern XYX.
val text = "abaca dedfd ghgig"
val p = """([a-z])(?!\1)[a-z]\1""".r
p.findAllIn(text).toArray
When I run the script I get:
aba, ded, ghg
And it should be:
aba, aca, ded, dfd, ghg, gig
It does not detect overlapped combinations.
You need to capture the whole pattern and put it inside a positive lookahead. The code in Scala will be the following:
See the online Scala demo
Note that the backreference will turn to
\2
as the group to check will have ID = 2 and Group 1 will contain the value you need to collect.