Skip to content

Commit 0c9ae85

Browse files
committed
add more tests for replace and fix readme
1 parent 3f97b6a commit 0c9ae85

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Diff for: README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#regexp2 - full featured regular expressions for Go
2-
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.
1+
# regexp2 - full featured regular expressions for Go
2+
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.
33

44
## Basis of the engine
55
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.
@@ -10,7 +10,7 @@ This is a go-gettable library, so install is easy:
1010
go get github.com/dlclark/regexp2/...
1111

1212
## Usage
13-
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.
13+
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.
1414

1515
```go
1616
re := regexp2.MustCompile(`Your pattern`, 0)

Diff for: replace_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,25 @@ func TestReplaceFunc_Groups(t *testing.T) {
148148
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
149149
}
150150
}
151+
152+
func TestReplace_RefNumsDollarAmbiguous(t *testing.T) {
153+
re := MustCompile("(123)hello(789)", None)
154+
res, err := re.Replace("123hello789", "$1456$2", -1, -1)
155+
if err != nil {
156+
t.Fatal(err)
157+
}
158+
if want, got := "$1456789", res; want != got {
159+
t.Fatalf("Wrong result: %s", got)
160+
}
161+
}
162+
163+
func TestReplace_NestedGroups(t *testing.T) {
164+
re := MustCompile(`(\p{Sc}\s?)?(\d+\.?((?<=\.)\d+)?)(?(1)|\s?\p{Sc})?`, None)
165+
res, err := re.Replace("$17.43 €2 16.33 £0.98 0.43 £43 12€ 17", "$2", -1, -1)
166+
if err != nil {
167+
t.Fatal(err)
168+
}
169+
if want, got := "17.43 2 16.33 0.98 0.43 43 12 17", res; want != got {
170+
t.Fatalf("Wrong result: %s", got)
171+
}
172+
}

0 commit comments

Comments
 (0)