Skip to content

regexp: greedy matching right-to-left vs left-to-right #110

@gopherbot

Description

@gopherbot

by stefanha:

regexp.MatchStrings() produces unexpected results which suggest that greedy
matching works right-to-left.  Conventional regular expression engines seem
to match left-to-right (sed, Vim, Perl, Python, ...).

The documentation does not state the intended behavior.  If it really
should work right-to-left, then a note in the documentation would help
reduce surprise.

What steps will reproduce the problem?
1. See the following test program:

package main

import "regexp"
import "fmt"

func main() {
        re, _ := regexp.Compile("(.*).*");
        a := re.MatchStrings("this is a test");
        fmt.Printf("%#v\n", a);
}

2. The following output is produced:

[]string{"this is a test", ""}

The .* outside of a group has matched the line.  I expect greedy matching
to work left-to-right, therefore the (.*) should match the line:

[]string{"this is a test", "this is a test"}

What is your $GOOS?  $GOARCH?

GOOS=linux GOARCH=386

Which revision are you sync'ed to?  (hg log -l 1)

3975:b51fd2d6c160

Simple comparisons:

$ echo this is a test | sed 's/\(.*\).*/\1/'
this is a test

$ python -c 'import re; print re.match("(.*).*", "this is a
test").groups()'
('this is a test',)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions