How to use multiple regex expressions for one string

huangapple go评论81阅读模式
英文:

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:

&lt;tag1&gt;hello
my name is Matt&lt;/tag1&gt;&lt;tag2&gt;goodbye!&lt;/tag2&gt;

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: &lt;(tag1|tag2)&gt;((.|\n)*)&lt;/(tag1|tag2)&gt;.

答案1

得分: 1

你正在使用的正则表达式也会错误地匹配具有不匹配的标签开头和结尾的文本(例如&lt;tag1&gt;Hello!&lt;/tag2&gt;)。如果你修改你的正则表达式如下所示,两个标签都会被匹配:

(&lt;tag1&gt;((.|\n)*)&lt;/tag1&gt;)|(&lt;tag2&gt;((.|\n)*)&lt;/tag2&gt;)

但正如其他人在评论中提到的,使用解析器可能是更好的解决方案。从外观上看,甚至一个XML解析器也可以帮助你处理你的用例,而你不需要编写新的语法。

英文:

The regex you're using will also incorrectly match text with mismatched tag openings and endings (e.g. &lt;tag1&gt;Hello!&lt;/tag2&gt;). If you modify your regex like the following, both tags will be matched:

(&lt;tag1&gt;((.|\n)*)&lt;/tag1&gt;)|(&lt;tag2&gt;((.|\n)*)&lt;/tag2&gt;)

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.

huangapple
  • 本文由 发表于 2022年10月8日 03:33:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/73991911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定