为了账号安全,请及时绑定邮箱和手机立即绑定

用于查找格式错误的引用字符串的正则表达式

用于查找格式错误的引用字符串的正则表达式

Go
BIG阳 2023-06-19 14:04:24
我正在尝试为具有起始引号但没有结束引号的子字符串构建正则表达式。下面是一个示例字符串"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


查看完整回答
反对 回复 2023-06-19
  • 1 回答
  • 0 关注
  • 100 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信