Skip to content

Golang doesn't support positive lookbehind assertion? #11

@thinkhy

Description

@thinkhy

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!")
        }
    }

Activity

StefanSchroeder

StefanSchroeder commented on Apr 8, 2016

@StefanSchroeder
Owner

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

thinkhy

thinkhy commented on Apr 9, 2016

@thinkhy
Author

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. 
sergeevabc

sergeevabc commented on Dec 25, 2016

@sergeevabc

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

StefanSchroeder commented on Jan 4, 2017

@StefanSchroeder
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @thinkhy@sergeevabc@StefanSchroeder

        Issue actions

          Golang doesn't support positive lookbehind assertion? · Issue #11 · StefanSchroeder/Golang-Regex-Tutorial