我有一个大字符串,其形式如下:
[' some text [ARG1: some inner text [1: some more text], and also [ other inner text [TAG: TAG_TYPE (0.99)]] ]', 'some more text ( some text in parentheses [2: words [ARG1: more words [ARGM-TYPE: even more nested words]]] [other text in square brackets []])']
我希望捕获单引号中的所有内容,我可以通过简单的
('(.*?)')
现在在这个捕获组中,我想捕获所有其他可能的组,包括可选的嵌套子组
我可以自己捕获一些子组,例如:
(\[ONTOTYPE: PERSON \((0\.(\d{1,4})\))\])
但我似乎错过了一些处理可选嵌套的基本见解。 如果我缺少一些概念,我欢迎链接到任何好的解释
我知道命名捕获组的概念,但我认为在这里使用它们只会增加混乱
作为参考,我当前的尝试如下:https://regex101.com/r/bzSCD0/1/
特别是,match 1中的组15有一个子字符串,该子字符串与其中一个表达式匹配,但没有进一步解析它
预计到达时间:
以下是一些输入和预期输出的示例:
让我们使用regex101页面中的一个:' [ ARG0 : Those ] [ R - ARG0 : who ] [ V : graduated ] [ ARG1 : from [0: the school ] ] were promoted from provincial secretary to titular adviser . '
-->{ARG0:Those, R-ARG0: who, V:graduated, ARG1:from, 0: the school, <rest of text>}
。我已经将匹配1转换为dict,键值对是groups1,而不是按匹配顺序
让我们使用列表开头的较大示例,并使用编号的捕获组:
[' 2 Architects : [ V : Stasov ] [ ARG1 : V. P. Melnikov ] [ ARG1 : A. ] [ ARGM - LOC : [2: I. Suzor P. Yu [ONTOTYPE: PERSON (0.851)] ] ] . Year of construction : [1: 1835 ] , 1895 - 1910 [ONTOTYPE: DATE (0.8774)] Style : Classicism [0: School of Law ] Classicism on [3: the Fontanka River [ONTOTYPE: WORK_OF_ART (0.8261)] ] , [4: 6 - Tchaikovsky Street ] , [5: 1 - Oruzhenik Fedorov Street ] , 2 - A. A. Rzhevsky House 1790 [ONTOTYPE: DATE (0.7046)] - [0: School of Law ][1: 1835 ] - arch . Stasov Vasily Petrovich [ONTOTYPE: PERSON (0.4863)] , arch . Melnikov Avraham Ivanovich [ONTOTYPE: PERSON (0.7781)] ( ? ) '
变成
group 1: 2 Architects
group 2: V
group 3: Stasov
group 4: ARG1
group 5: V.P. Melnikov
group 6: ARG1
group 7: A
group 8: ARGM-LOC
group 9: 2
group 10: I. Suzor P. Yu
group 11: ONTOTYPE: PERSON (0.851)
group 12: Year of construction
group 13: 1
group 14: 1835
等等
更新:我现在构建了第二个版本的正则表达式:https://regex101.com/r/bzSCD0/2/
我们希望首先捕获所有简单的组(这种情况经常发生),然后使用backreference尝试在其他组中选择性地捕获它们。
仍然不知道如何将所有这些应用于单引号之间的字符串(即('(.*?)')
组)
似乎我需要某种方法来避免(.*?)
捕获所有组:一旦找到匹配项,正则表达式引擎就不会检查它是否符合不同的模式
请尝试下面的程序
是否在此程序(Demo)中使用正则表达式
输出
相关问题 更多 >
编程相关推荐