简体   繁体   English

正则表达式,提取括号之间的值

[英]regexp, Extract value between parentheses

example:例子:

"today  [(["hi"],{"my"})],["ok"],("good")),(["gg"],["fire"])] nice game [(["1"],{"2"})],["3"],("4")),(["5"],["6"])] end."

-> ->

[(["hi"],{"my"})],["ok"],("good")),(["gg"],["fire"])]
[(["1"],{"2"})],["3"],("4")),(["5"],["6"])]

在此处输入图像描述 In this case, there is a noise in the middle.在这种情况下,中间有噪音。

It doesn't work in the usual way它不能以通常的方式工作

Please give me an idea of extracting the value between parentheses in another way请给我一个想法,以另一种方式提取括号之间的值

I love regex, almost as much as I hate them.我喜欢正则表达式,几乎和讨厌它们一样多。 Writing an effective regex can take a problem from impossible to trivial.编写有效的正则表达式可以使问题变得微不足道。

Knowing how to iterate one is very important.知道如何迭代一个是非常重要的。 Let me teach a man to fish.让我教一个人钓鱼。

Consider using an online regex tool: https://regex101.com/考虑使用在线正则表达式工具: https://regex101.com/

Input your string:输入你的字符串:

image of me adding in the text我在文本中添加的图像

Then try to Match it.然后尝试匹配它。 I did with the following \([^()]+\)我做了以下\([^()]+\)

That is:那是:

  1. escape the parenthesis you want to match on \( and \) because they are special symbols.转义要在\(\)上匹配的括号,因为它们是特殊符号。
  2. You know you want to match everything that isn't a parenthesis between them.您知道您想要匹配它们之间不是括号的所有内容。 So I use a no-match clause by doing [^...] where the elipsis are all the characters I don't want to match.所以我通过[^...]使用不匹配子句,其中省略号是我不想匹配的所有字符。 I then add my parenthesis raw in there [^()]然后我在那里添加我的括号[^()]
  3. Finally.最后。 I want to have 1 or more of those so I use + .我想拥有其中的 1 个或更多,所以我使用+

Thus I have my awnser.这样我就有了我的遮阳篷。

image of me matching the text我与文本匹配的图像

edit: cannot post images because of rep编辑:由于代表无法发布图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM