英文:
Can you regexp.Split a string with regexp not containing word in golang?
问题
我写了一个函数,它接受一个字符串列表,列表项分隔符正则表达式和范围分隔符正则表达式,如下所示:
func ParseList(list string, ls, rs *regexp.Regexp) (...) {
...
for _, item := range ls.Split(list, -1) {
for _, rng := range rs.Split(item, -1) {
range_start, _ := strconv.ParseInt(rng[0])
range_end := range_start
if len(rng) > 1 {
range_end, _ = strconv.ParseInt(rng[1])
}
... 保存或分析范围
}
}
...
}
输入字符串可以是以下形式之一:
1,3,5-10,15-20
1;3;5-10;15-20
1 3 5 to 10 15 to 20
所以对于第一个例子,正则表达式很简单,但是第三个例子会有问题,因为列表分隔符是空格,范围分隔符也是空格。
更新:
所以我按照Trung Duong的建议,从"splitter"切换到"extractor"正则表达式,如下所示:
func ParseList(list string, ex, rs *regexp.Regexp) (...) {
...
for _, item := range ex.FindAllString(list, -1) {
for _, rng := range rs.Split(item, -1) {
range_start, _ := strconv.ParseInt(rng[0])
range_end := range_start
if len(rng) > 1 {
range_end, _ = strconv.ParseInt(rng[1])
}
... 保存或分析范围
}
}
...
}
现在我可以使用(\d+-\d+)|(\d+)
或(\d+ to \d+)|(\d+)
来提取范围,然后再进行分割。
英文:
I write a function which takes a string list of numbers, list items separator reg and range separator reg like:
func ParseList(list string, ls, rs *regexp.Regexp) (...) {
...
for _, item := range ls.Split(list, -1) {
for _, rng := range rs.Split(item, -1) {
range_start, _ := strconv.ParseInt(rng[0])
range_end := range_start
if len(rng) > 1 {
range_end, _ = strconv.ParseInt(rng[1])
}
... save or analyse range
}
}
...
}
Input string could be, but not limited to:
1,3,5-10,15-20
1;3;5-10;15-20
1 3 5 to 10 15 to 20
So for first example regexps are easy, but third gives a problem, because list separator is " " and range separator also has " ".
UPDATE:
So i followed Trung Duong's suggestion and switched from "splitter" to "extractor" regexp like this:
func ParseList(list string, ex, rs *regexp.Regexp) (...) {
...
for _, item := range ex.FindAllString(list, -1) {
for _, rng := range rs.Split(item, -1) {
range_start, _ := strconv.ParseInt(rng[0])
range_end := range_start
if len(rng) > 1 {
range_end, _ = strconv.ParseInt(rng[1])
}
... save or analyse range
}
}
...
}
So now i can use (\d+-\d+)|(\d+)
or (\d+ to \d+)|(\d+)
to extract ranges, and then split them.
答案1
得分: 2
你可以使用这个模式:(\d+\s+to\s+\d+)|(\d+)
在这里查看正则表达式演示链接。
答案2
得分: 0
这是你需要的算法。我们可以通过迭代来实现。
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
inputString := "1 2 7 to 10 15 16 to 20"
// 通过空格将字符串拆分为单个元素
elements := strings.Split(inputString, " ")
// 初始化一个切片来存储输出
output := []string{}
// 遍历元素
i := 0
for i < len(elements) {
// 如果元素包含 "to",则表示它是一个范围
if strings.Contains(elements[i], "to") {
// 获取范围的起始和结束
rangeEnds := strings.Split(elements[i], "to")
start, _ := strconv.Atoi(rangeEnds[0])
end, _ := strconv.Atoi(rangeEnds[1])
// 将范围添加到输出
output = append(output, fmt.Sprintf("%d to %d", start, end))
i++
} else {
// 如果元素不是范围,则直接将其添加到输出
output = append(output, elements[i])
i++
}
}
// 打印输出
for _, element := range output {
fmt.Println(element)
}
}
输出:
1
2
7 to 10
15
16 to 20
英文:
Here is the algorithm You need.
We can simply do this with the help of iterations.
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
inputString := "1 2 7 to 10 15 16 to 20"
// Split the string by space to get individual elements
elements := strings.Split(inputString, " ")
// Initialize a slice to store the output
output := []string{}
// Iterate through the elements
i := 0
for i < len(elements) {
// If the element contains "to", it means it's a range
if strings.Contains(elements[i], "to") {
// Get the start and end of the range
rangeEnds := strings.Split(elements[i], "to")
start, _ := strconv.Atoi(rangeEnds[0])
end, _ := strconv.Atoi(rangeEnds[1])
// Add the range to the output
output = append(output, fmt.Sprintf("%d to %d", start, end))
i++
} else {
// If the element is not a range, simply add it to the output
output = append(output, elements[i])
i++
}
}
// Print the output
for _, element := range output {
fmt.Println(element)
}
}
OUTPUT:
1
2
7 to 10
15
16 to 20
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论