# \[Feature request\] Regex findAll with overlap

**URL:** <https://discuss.kotlinlang.org/t/feature-request-regex-findall-with-overlap/27729>\
**Category:** Language Design\
**Created:** [December 1, 2023, 8:04am UTC](https://discuss.kotlinlang.org/t/feature-request-regex-findall-with-overlap/27729 "2023-12-01T08:04:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DownloadPizza](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@DownloadPizza](https://discuss.kotlinlang.org/u/DownloadPizza)\
**Post date:** [December 1, 2023, 8:04am UTC](https://discuss.kotlinlang.org/t/feature-request-regex-findall-with-overlap/27729/1 "2023-12-01T08:04:54Z")

</div>

I ran into an annoyance today where findAll did not match overlapping parts. This is sane, to be fair. But having alternative behavior would be nice.

Can we get a findAllWithOverlap defined as follows?:

```kt
fun findAllWithOverlap(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> {
        if (startIndex < 0 || startIndex > input.length) {
            throw IndexOutOfBoundsException("Start index out of bounds: $startIndex, input length: ${input.length}")
        }
        return generateSequence({ find(input, startIndex) }, { find(input, it.range.first + 1) })
    }

```

Making a pull request seems like a pain since I would need to write the JS impl as well (maybe, idk how the kotlin repo works) so I thought id just take away most of the work and the core team can decide.

---

<div class="post-metadata">

**Author:** ![alexis.manin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexis.manin/32/8381_2.png) [@alexis.manin](https://discuss.kotlinlang.org/u/alexis.manin)\
**Post date:** [December 1, 2023, 7:04pm UTC](https://discuss.kotlinlang.org/t/feature-request-regex-findall-with-overlap/27729/2 "2023-12-01T19:04:09Z")

</div>

I suppose you’ve worked on this year advent of code then ?

---

<div class="post-metadata">

**Author:** ![V2](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/v2/32/10235_2.png) [@V2](https://discuss.kotlinlang.org/u/V2)\
**Post date:** [December 12, 2023, 6:50am UTC](https://discuss.kotlinlang.org/t/feature-request-regex-findall-with-overlap/27729/3 "2023-12-12T06:50:21Z")

</div>

I believe this function is not necessary, because we can easily “enable overlapping” by using look-ahead in regex.

#### Example

Suppose we have haystack string: `1234 5678`

Using a simple regex `\d\d\d` on the haystack would give us two matches `123` and `567`.

By simply wrapping the regex with a positive-lookahead, plus a capturing group, i.e.  
`(?=(\d\d\d))`

this would then return 4 matches, where each group 1 would contain the digits, giving us  
`123`, `234`, `567` and `678`

#### Example in Kotlin

Code ([playground](https://pl.kotl.in/EQfjMvudZ)):

```auto
println(
    Regex("\\d\\d\\d")
        .findAll("1234 5678")
    	.map{it.groupValues[0]}
        .joinToString()
)
  
println(
    Regex("(?=(\\d\\d\\d))")
        .findAll("1234 5678")
    	.map{it.groupValues[1]}
        .joinToString()
)

```

Execution result:

```auto
123, 567
123, 234, 567, 678

```
