Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A bug when .* in the content to match #49

Closed
wgplt opened this issue Apr 27, 2022 · 1 comment
Closed

A bug when .* in the content to match #49

wgplt opened this issue Apr 27, 2022 · 1 comment

Comments

@wgplt
Copy link

wgplt commented Apr 27, 2022

The code that caused the error:

image

Why nil ?

This should be the right result:

image

Sample code:

`package main

import (
"fmt"

"github.com/dlclark/regexp2"

)

func main() {

r, err := regexp2.Compile(`(?<=1234\.\*56).*(?=890)`, regexp2.Compiled)

if err != nil {
	panic(err)
}

m, err := r.FindStringMatch(`1234.*567890`)
if err != nil {
	panic(err)
}

fmt.Println(m)

}`

@dlclark dlclark closed this as completed in 8fc3b60 May 2, 2022
@dlclark
Copy link
Owner

dlclark commented May 2, 2022

This was a heck of a bug!

Basically it required a specific shape to string literals within the pattern broken up with escape sequences just right so that when the pattern parser concatenated them all it re-used the underlying rune slice. The rune slice was an extract from the pattern itself, so the append calls were smashing the contents of the pattern accidentally. This meant the pattern was being parsed into a weird state and wouldn't match what you wanted.

The fix was to copy the runes from the pattern into a new slice instead of just slicing from the pattern itself. A simple fix after hours of debugging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants