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

Iterate over all matches? #7

Open
brettdanger opened this issue Feb 13, 2016 · 4 comments
Open

Iterate over all matches? #7

brettdanger opened this issue Feb 13, 2016 · 4 comments

Comments

@brettdanger
Copy link

Is there a way to iterate over all matches (not capture groups)? SImilar to replaceAll but matchall? I can't figure out how to iterate over them. This package doesnt seem to be active though so I may be out of luck.

@glenn-brown
Copy link
Owner

One can iterate over matches with a range-based for loop:
for _, match := range matches {
// use match
}
On Feb 12, 2016 11:41 PM, "Brett Dangerfield" [email protected]
wrote:

Is there a way to iterate over all matches (not capture groups)? SImilar
to replaceAll but matchall? I can't figure out how to iterate over them.
This package doesnt seem to be active though so I may be out of luck.


Reply to this email directly or view it on GitHub
https://github.com/glenn-brown/golang-pkg-pcre/issues/7.[image: Web Bug
from
https://github.com/notifications/beacon/AAgC2yQagqroUOL9lxBZPi21V7c9oxY5ks5pjtW9gaJpZM4HZlfU.gif]

@brettdanger
Copy link
Author

What method returns an array or slice of Matches?

searchstring := "This is a test"
    re := pcre.MustCompile("\\w+\\s", 0)

    matches := re.MatcherString(searchstring, 0)

    for _, match := range matches {
        // use match
        fmt.Printf("Match -- %v\n", match.GroupString(0))
    }

That returns:
./retest.go:15: cannot range over matches (type *pcre.Matcher)

I don't see anything in these docs that returns an array of Match objects.

https://godoc.org/github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre#Matcher.Matches

@mdwhatcott
Copy link

@brettdanger - There is not yet an equivalent of preg_match_all (from PHP) in this library or even in pcre. What this library could do is continue to call pcre_exec passing in subsequent offsets to extract all matches.

Reference: http://stackoverflow.com/a/15459085

@glenn-brown - Any plans to implement some sort of MatchAll() method?

@mdwhatcott
Copy link

I actually just implemented it (feel free to commit this if you like it @glenn-brown):

func (re Regexp) MatchAll(subject []byte, flags int) [][]byte {
    m := re.Matcher(subject, 0)
    all := [][]byte{}
    for m.Match(subject, flags) {
        all = append(all, subject[m.ovector[0]:m.ovector[1]])
        subject = subject[m.ovector[1]:]
    }
    return all
}

Here's a test to verify the behavior:

func TestMatchAll(t *testing.T) {
    re := MustCompile(`food[\W]`, 0)
    result := re.MatchAll([]byte("food, glorious food!"), 0)
    if len(result) != 2 {
        t.Errorf("Wanted 2 matches, got [%d] instead.", len(result))
    } else if string(result[0]) != "food," {
        t.Errorf("Want [food,] Got: [%s]\n", string(result[0]))
    } else if string(result[1]) != "food!" {
        t.Error("Want [food!] Got: [%s]\n", string(result[1]))
    }
}

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