Skip to content

Commit

Permalink
add more tests for replace and fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
dlclark committed Mar 23, 2017
1 parent 3f97b6a commit 0c9ae85
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#regexp2 - full featured regular expressions for Go
Regexp2 is a feature-rich RegExp engine for Go. It doesn't have constant time guarantees like the built-in `regexp` package, but it allows backtracking and is compatible with Perl5 and .NET. You'll likely be better off with the RE2 engine from the 'regexp' package and should only use this if you need to write very complex patterns or require compatibility with .NET.
# regexp2 - full featured regular expressions for Go
Regexp2 is a feature-rich RegExp engine for Go. It doesn't have constant time guarantees like the built-in `regexp` package, but it allows backtracking and is compatible with Perl5 and .NET. You'll likely be better off with the RE2 engine from the `regexp` package and should only use this if you need to write very complex patterns or require compatibility with .NET.

## Basis of the engine
The engine is ported from the .NET framework's System.Text.RegularExpressions.Regex engine. That engine was open sourced in 2015 under the MIT license. There are some fundamental differences between .NET strings and Go strings that required a bit of borrowing from the Go framework regex engine as well. I cleaned up a couple of the dirtier bits during the port (regexcharclass.cs was terrible), but the parse tree, code emmitted, and therefore patterns matched should be identical.
Expand All @@ -10,7 +10,7 @@ This is a go-gettable library, so install is easy:
go get github.com/dlclark/regexp2/...

## Usage
Usage is similar to the Go `regexp` package. Just like in `regexp`, you start by converting a regex into a state machine via the `Compile` or `MustCompile` methods. They ultimately do the same thing, but `MustCompile` will panic if the regex is invalid. You can then use the provided Regexp struct to find matches repeatedly. A `Regexp` struct is safe to use across goroutines.
Usage is similar to the Go `regexp` package. Just like in `regexp`, you start by converting a regex into a state machine via the `Compile` or `MustCompile` methods. They ultimately do the same thing, but `MustCompile` will panic if the regex is invalid. You can then use the provided `Regexp` struct to find matches repeatedly. A `Regexp` struct is safe to use across goroutines.

```go
re := regexp2.MustCompile(`Your pattern`, 0)
Expand Down
22 changes: 22 additions & 0 deletions replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,25 @@ func TestReplaceFunc_Groups(t *testing.T) {
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
}
}

func TestReplace_RefNumsDollarAmbiguous(t *testing.T) {
re := MustCompile("(123)hello(789)", None)
res, err := re.Replace("123hello789", "$1456$2", -1, -1)
if err != nil {
t.Fatal(err)
}
if want, got := "$1456789", res; want != got {
t.Fatalf("Wrong result: %s", got)
}
}

func TestReplace_NestedGroups(t *testing.T) {
re := MustCompile(`(\p{Sc}\s?)?(\d+\.?((?<=\.)\d+)?)(?(1)|\s?\p{Sc})?`, None)
res, err := re.Replace("$17.43 €2 16.33 £0.98 0.43 £43 12€ 17", "$2", -1, -1)
if err != nil {
t.Fatal(err)
}
if want, got := "17.43 2 16.33 0.98 0.43 43 12 17", res; want != got {
t.Fatalf("Wrong result: %s", got)
}
}

0 comments on commit 0c9ae85

Please sign in to comment.