mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat: add RegexMatchAllGroups
This commit is contained in:
@@ -66,6 +66,9 @@ import (
|
|||||||
- [Ellipsis](#Ellipsis)
|
- [Ellipsis](#Ellipsis)
|
||||||
- [Shuffle](#Shuffle)
|
- [Shuffle](#Shuffle)
|
||||||
- [Rotate](#Rotate)
|
- [Rotate](#Rotate)
|
||||||
|
- [TemplateReplace](#TemplateReplace)
|
||||||
|
- [RegexMatchAllGroups](#RegexMatchAllGroups)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1692,4 +1695,37 @@ func main() {
|
|||||||
// Output:
|
// Output:
|
||||||
// Hello, my name is Bob, I'm 20 years old.
|
// Hello, my name is Bob, I'm 20 years old.
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="RegexMatchAllGroups">RegexMatchAllGroups</span>
|
||||||
|
|
||||||
|
<p>使用正则表达式匹配字符串中的所有子组并返回结果。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RegexMatchAllGroups(pattern, str string) [][]string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[Run]()</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -66,6 +66,8 @@ import (
|
|||||||
- [Ellipsis](#Ellipsis)
|
- [Ellipsis](#Ellipsis)
|
||||||
- [Shuffle](#Shuffle)
|
- [Shuffle](#Shuffle)
|
||||||
- [Rotate](#Rotate)
|
- [Rotate](#Rotate)
|
||||||
|
- [TemplateReplace](#TemplateReplace)
|
||||||
|
- [RegexMatchAllGroups](#RegexMatchAllGroups)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1665,13 +1667,13 @@ func main() {
|
|||||||
|
|
||||||
<p>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!".</p>
|
<p>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!".</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func TemplateReplace(template string, data map[string]string string
|
func TemplateReplace(template string, data map[string]string string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>example:<span style="float:right;display:inline-block;">[运行]()</span></b>
|
<b>example:<span style="float:right;display:inline-block;">[Run]()</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -1693,4 +1695,37 @@ func main() {
|
|||||||
// Output:
|
// Output:
|
||||||
// Hello, my name is Bob, I'm 20 years old.
|
// Hello, my name is Bob, I'm 20 years old.
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="RegexMatchAllGroups">RegexMatchAllGroups</span>
|
||||||
|
|
||||||
|
<p>Matches all subgroups in a string using a regular expression and returns the result.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RegexMatchAllGroups(pattern, str string) [][]string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>example:<span style="float:right;display:inline-block;">[Run]()</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -727,3 +727,11 @@ func TemplateReplace(template string, data map[string]string) string {
|
|||||||
|
|
||||||
return result
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -739,3 +739,17 @@ func ExampleTemplateReplace() {
|
|||||||
// Output:
|
// Output:
|
||||||
// Hello, my name is Bob, I'm 20 years old.
|
// 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]
|
||||||
|
}
|
||||||
|
|||||||
@@ -810,3 +810,46 @@ func TestTemplateReplace(t *testing.T) {
|
|||||||
assert.Equal(expected, result)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user