我正在尝试为具有起始引号但没有结束引号的子字符串构建正则表达式。下面是一个示例字符串"The fish was 2 long. I said: Give me that fish!.The fish was 2" long. I said: Give me that fish!."The fish was 2 long. I said: Give me that fish!."The fish "was 2 long". I said: "Give me that fish!.所以表达式的有效输出是String 1 - The fish was 2 long. I said: Give me that fish!String 2 - long. I said: Give me that fish!.String 3 - No outputString 4 - Give me that fish!.任何想法如何构建正则表达式。
1 回答
喵喵时光机
TA贡献1846条经验 获得超7个赞
可以用正则表达式做所有事情.. 需要更多时间来完善解决方案。但是使用一些字符串函数会让你轻松一些。
希望这可以帮助!!
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
str := `
"The fish was 2 long. I said: Give me that fish!.
The fish was 2" long. I said: Give me that fish!.
"The fish was 2 long. I said: Give me that fish!."
""sdfsdf
`
splitStr := strings.Split(str, "\n")
re := regexp.MustCompile(`"{1}[A-Za-z0-9 .:!]+"{0}[A-Za-z0-9 .:!]+$`)
for _,v := range splitStr {
if len(strings.Trim(v, " ")) >= 0{
// some extra check
if strings.Count(v, "\"") % 2 == 1 {
fmt.Println( re.FindString(v))
}
}
}
}
此处提供代码示例: https: //play.golang.org/p/LYte6FRocv3
- 1 回答
- 0 关注
- 100 浏览
添加回答
举报
0/150
提交
取消