英文:
How to use multiple regex expressions for one string
问题
有没有办法使用Golang和正则表达式来存储这两种类型的“标签”?
以下是所说标签的示例:
<tag1>hello
my name is Matt</tag1><tag2>goodbye!</tag2>
我尝试了很多方法,但每次尝试都只能捕获“tag1”。
以下是我使用的正则表达式:<(tag1|tag2)>((.|\n)*)</(tag1|tag2)>
。
英文:
Is there any way to store both types of these "tags" by using Golang and regex.
Here is an example of said tags:
<tag1>hello
my name is Matt</tag1><tag2>goodbye!</tag2>
I have tried many approaches at this, however on each try it only manages to pick up "tag1".
Here is the regular expression I am using: <(tag1|tag2)>((.|\n)*)</(tag1|tag2)>
.
答案1
得分: 1
你正在使用的正则表达式也会错误地匹配具有不匹配的标签开头和结尾的文本(例如<tag1>Hello!</tag2>
)。如果你修改你的正则表达式如下所示,两个标签都会被匹配:
(<tag1>((.|\n)*)</tag1>)|(<tag2>((.|\n)*)</tag2>)
但正如其他人在评论中提到的,使用解析器可能是更好的解决方案。从外观上看,甚至一个XML解析器也可以帮助你处理你的用例,而你不需要编写新的语法。
英文:
The regex you're using will also incorrectly match text with mismatched tag openings and endings (e.g. <tag1>Hello!</tag2>
). If you modify your regex like the following, both tags will be matched:
(<tag1>((.|\n)*)</tag1>)|(<tag2>((.|\n)*)</tag2>)
But as mentioned by someone else in the comments, using a parser is probably the better solution here. By the looks of it even an XML parser could help you with your use case and you don't need to write new grammar.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论