我想查找在集合中分组的索引数组元素(逗号分隔),搜索结果应该是这样的(请参见下面的文件数据示例)——
[ 'foo[0]', 'foo[1]', 'foo[2]', ..., 'foo[10]']
['foobar0[0]', 'foobar0[1]', 'foobar0[2]' ..., 'foobar0[98]']
[ 'bas[0]', 'bas[1]', 'bas[2]' ..., 'bas[99]']
我有一个文本文件,其中这些显示为跨越多行的“集合”,集合按{..}分组(如下所示)——
{foo[0], foo[1], foo[2], foo[3]...\n
foo[10]}, {fooba0[0], foobar0[1], foobar0[2],....\n
foobar0[98], foobar0[99]}, {bas[0], bas[1], bas[2]...\n
bas[99]}
我用来搜索数组元素的一般表达式是-
re.findall('[a-z][A-Z]+[0-9]+\[[0-9]+\]', <list item>)
在yacc
中,这将转化为类似于-
array_element_token: [a-z][A-Z]+[0-9]+\[[0-9]+\]
array_items_continued: array_items_continued |
array_element_token ','
arrays_items: '{' array_items_continued array_element_token '},'
构建我不知道如何使用python正则表达式创建递归规则
你可以用
参见Python demo,结果为
[['foo[0]', 'foo[1]', 'foo[2]', 'foo[3]', 'foo[10]'], ['fooba0[0]', 'foobar0[1]', 'foobar0[2]', 'foobar0[98]', 'foobar0[99]'], ['bas[0]', 'bas[1]', 'bas[2]', 'bas[99]']]
{[^{}]*}
正则表达式提取{
和}
之间的所有子字符串,然后\w+\[\d+]
提取与以下序列匹配的所有子字符串:\w+
-1+字母、数字、_
字符\[
-a[
字符\d+
-1+位]
-a]
字符李>相关问题 更多 >
编程相关推荐