diff --git a/docs/api/packages/strutil.md b/docs/api/packages/strutil.md index 94fe18e..a108716 100644 --- a/docs/api/packages/strutil.md +++ b/docs/api/packages/strutil.md @@ -66,6 +66,9 @@ import ( - [Ellipsis](#Ellipsis) - [Shuffle](#Shuffle) - [Rotate](#Rotate) +- [TemplateReplace](#TemplateReplace) +- [RegexMatchAllGroups](#RegexMatchAllGroups) +
@@ -1692,4 +1695,37 @@ func main() { // Output: // Hello, my name is Bob, I'm 20 years old. } +``` + +### RegexMatchAllGroups + +

使用正则表达式匹配字符串中的所有子组并返回结果。

+ +函数签名: + +```go +func RegexMatchAllGroups(pattern, str string) [][]string +``` + +示例:[Run]() + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + pattern := `(\w+\.+\w+)@(\w+)\.(\w+)` + str := "Emails: john.doe@example.com and jane.doe@example.com" + + result := strutil.RegexMatchAllGroups(pattern, str) + + fmt.Println(result[0]) + fmt.Println(result[1]) + + // Output: + // [john.doe@example.com john.doe example com] + // [jane.doe@example.com jane.doe example com] +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/strutil.md b/docs/en/api/packages/strutil.md index 1ed8551..d6575d7 100644 --- a/docs/en/api/packages/strutil.md +++ b/docs/en/api/packages/strutil.md @@ -66,6 +66,8 @@ import ( - [Ellipsis](#Ellipsis) - [Shuffle](#Shuffle) - [Rotate](#Rotate) +- [TemplateReplace](#TemplateReplace) +- [RegexMatchAllGroups](#RegexMatchAllGroups)
@@ -1665,13 +1667,13 @@ func main() {

Replaces the placeholders in the template string with the corresponding values in the data map.The placeholders are enclosed in curly braces, e.g. {key}. for example, the template string is "Hello, {name}!", and the data map is {"name": "world"}, the result will be "Hello, world!".

-函数签名: +Signature: ```go func TemplateReplace(template string, data map[string]string string ``` -example:[运行]() +example:[Run]() ```go import ( @@ -1693,4 +1695,37 @@ func main() { // Output: // Hello, my name is Bob, I'm 20 years old. } +``` + +### RegexMatchAllGroups + +

Matches all subgroups in a string using a regular expression and returns the result.

+ +Signature: + +```go +func RegexMatchAllGroups(pattern, str string) [][]string +``` + +example:[Run]() + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + pattern := `(\w+\.+\w+)@(\w+)\.(\w+)` + str := "Emails: john.doe@example.com and jane.doe@example.com" + + result := strutil.RegexMatchAllGroups(pattern, str) + + fmt.Println(result[0]) + fmt.Println(result[1]) + + // Output: + // [john.doe@example.com john.doe example com] + // [jane.doe@example.com jane.doe example com] +} ``` \ No newline at end of file diff --git a/strutil/string.go b/strutil/string.go index daf2a43..b1d1b7e 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -727,3 +727,11 @@ func TemplateReplace(template string, data map[string]string) string { return result } + +// RegexMatchAllGroups Matches all subgroups in a string using a regular expression and returns the result. +// Play: todo +func RegexMatchAllGroups(pattern, str string) [][]string { + re := regexp.MustCompile(pattern) + matches := re.FindAllStringSubmatch(str, -1) + return matches +} diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index ac4d946..9840876 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -739,3 +739,17 @@ func ExampleTemplateReplace() { // Output: // Hello, my name is Bob, I'm 20 years old. } + +func ExampleRegexMatchAllGroups() { + pattern := `(\w+\.+\w+)@(\w+)\.(\w+)` + str := "Emails: john.doe@example.com and jane.doe@example.com" + + result := RegexMatchAllGroups(pattern, str) + + fmt.Println(result[0]) + fmt.Println(result[1]) + + // Output: + // [john.doe@example.com john.doe example com] + // [jane.doe@example.com jane.doe example com] +} diff --git a/strutil/string_test.go b/strutil/string_test.go index dd83d7a..e7e068b 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -810,3 +810,46 @@ func TestTemplateReplace(t *testing.T) { assert.Equal(expected, result) }) } + +func TestRegexMatchAllGroups(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestRegexMatchAllGroups") + + tests := []struct { + pattern string + str string + expected [][]string + }{ + { + pattern: `(\w+\.+\w+)@(\w+)\.(\w+)`, + str: "Emails: john.doe@example.com and jane.doe@example.com", + expected: [][]string{{"john.doe@example.com", "john.doe", "example", "com"}, {"jane.doe@example.com", "jane.doe", "example", "com"}}, + }, + { + pattern: `(\d+)`, + str: "No numbers here!", + expected: nil, + }, + { + pattern: `(\d{3})-(\d{2})-(\d{4})`, + str: "My number is 123-45-6789", + expected: [][]string{{"123-45-6789", "123", "45", "6789"}}, + }, + { + pattern: `(\w+)\s(\d+)`, + str: "Item A 123, Item B 456", + expected: [][]string{{"A 123", "A", "123"}, {"B 456", "B", "456"}}, + }, + { + pattern: `(\d{2})-(\d{2})-(\d{4})`, + str: "Dates: 01-01-2020, 12-31-1999", + expected: [][]string{{"01-01-2020", "01", "01", "2020"}, {"12-31-1999", "12", "31", "1999"}}, + }, + } + + for _, tt := range tests { + result := RegexMatchAllGroups(tt.pattern, tt.str) + assert.Equal(tt.expected, result) + } +}