猿问

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

我正在尝试为具有起始引号但没有结束引号的子字符串构建正则表达式。下面是一个示例字符串


"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 output

String 4 - Give me that fish!.

任何想法如何构建正则表达式。


BIG阳
浏览 101回答 1
1回答

喵喵时光机

可以用正则表达式做所有事情.. 需要更多时间来完善解决方案。但是使用一些字符串函数会让你轻松一些。希望这可以帮助!!package mainimport ("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
随时随地看视频慕课网APP

相关分类

Go
我要回答