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

Golang doesn't support positive lookbehind assertion? #11

Closed
thinkhy opened this issue Apr 2, 2016 · 4 comments
Closed

Golang doesn't support positive lookbehind assertion? #11

thinkhy opened this issue Apr 2, 2016 · 4 comments

Comments

@thinkhy
Copy link

thinkhy commented Apr 2, 2016

Seems Golang doesn't support positive lookbehind assertion, below code output "No matched!":

   package main
    import (
        "fmt"
        "regexp"
    )
    func main() {
        s := "abcdef"

        if matched, str := regexp.MatchString(`(?<=abc)def`, s); matched {
            fmt.Println("It's matched ", str)
        } else {
            fmt.Println("Nothing matched!")
        }
    }
@StefanSchroeder
Copy link
Owner

That is correct. This is not a bug. Refer to
https://github.com/google/re2/wiki/Syntax
to check the supported syntax.

@thinkhy
Copy link
Author

thinkhy commented Apr 9, 2016

Thanks for your answer. I also notice Russ Cox's statement in Golang-nuts

The lack of generalized assertions, like the lack of backreferences, 
is not a statement on our part about regular expression style.  It is 
a consequence of not knowing how to implement them efficiently.  If 
you can implement them while preserving the guarantees made by the 
current package regexp, namely that it makes a single scan over the 
input and runs in O(n) time, then I would be happy to review and 
approve that CL.  However, I have pondered how to do this for five 
years, off and on, and gotten nowhere. 

@thinkhy thinkhy closed this as completed Apr 9, 2016
@sergeevabc
Copy link

sergeevabc commented Dec 25, 2016

Oh, fellas, it makes life so harder.
For example, (\w+)(\.\w+)+(?!.*(\w+)(\.\w+)+) to get a filename from URL does not work.
More or less workaround is [^\/?#]*\.[^\/?#]*(\?.*)?(\#.*)?$.

@StefanSchroeder
Copy link
Owner

When retrieving a filename from a URL I would use 'split' at '/' and then take the last item. No need for regex.

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

3 participants