aboutsummaryrefslogtreecommitdiff
path: root/src/regexp
diff options
context:
space:
mode:
authorMatthew Broberg <gogetmb@gmail.com>2017-07-15 17:40:29 -0600
committerBrad Fitzpatrick <bradfitz@golang.org>2018-06-12 22:37:01 +0000
commit3885e864114f9e45ed7d4322e0d802b897124c37 (patch)
treebe05d407bf064ce3e8629e3165b69723cb780193 /src/regexp
parent2cf9732e8aae92edfa55a8d0e3cebea9154aced7 (diff)
downloadgo-3885e864114f9e45ed7d4322e0d802b897124c37.tar.gz
go-3885e864114f9e45ed7d4322e0d802b897124c37.zip
regexp: add QuoteMeta example
Change-Id: I0bbb53cad9a7c464ab1cfca381128f33496813ff Reviewed-on: https://go-review.googlesource.com/49130 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/regexp')
-rw-r--r--src/regexp/example_test.go6
-rw-r--r--src/regexp/regexp.go4
2 files changed, 8 insertions, 2 deletions
diff --git a/src/regexp/example_test.go b/src/regexp/example_test.go
index 0bf1f6bee7..eb8cd4ea94 100644
--- a/src/regexp/example_test.go
+++ b/src/regexp/example_test.go
@@ -38,6 +38,12 @@ func ExampleMatchString() {
// false error parsing regexp: missing closing ): `a(b`
}
+func ExampleQuoteMeta() {
+ fmt.Println(regexp.QuoteMeta("Escaping symbols like: .+*?()|[]{}^$"))
+ // Output:
+ // Escaping symbols like: \.\+\*\?\(\)\|\[\]\{\}\^\$
+}
+
func ExampleRegexp_FindString() {
re := regexp.MustCompile("foo.?")
fmt.Printf("%q\n", re.FindString("seafood fool"))
diff --git a/src/regexp/regexp.go b/src/regexp/regexp.go
index 09faced8f3..0d10aa1e22 100644
--- a/src/regexp/regexp.go
+++ b/src/regexp/regexp.go
@@ -616,9 +616,9 @@ func init() {
}
}
-// QuoteMeta returns a string that quotes all regular expression metacharacters
+// QuoteMeta returns a string that escapes all regular expression metacharacters
// inside the argument text; the returned string is a regular expression matching
-// the literal text. For example, QuoteMeta(`[foo]`) returns `\[foo\]`.
+// the literal text.
func QuoteMeta(s string) string {
// A byte loop is correct because all metacharacters are ASCII.
var i int