gpt4 book ai didi

python - 获取 'package' 和 'endpackage' 可选字符串之外的结构名称列表

转载 作者:行者123 更新时间:2023-12-04 07:41:40 25 4
gpt4 key购买 nike

我正在尝试获取 package 之外的结构名称和 endpackage可选字符串。
如果没有 packageendpackage字符串,则脚本应返回所有结构名称。
这是我的脚本:

import re

a = """
package new;

typedef struct packed
{
logic a;
logic b;
} abc_y;

typedef struct packed
{
logic a;
logic b;
} abc_t;

endpackage

typedef struct packed
{
logic a;
logic b;
} abc_x;

"""

print(re.findall(r'(?!package)*.*?typedef\s+struct\s+packed\s*{.*?}\s*(\w+);.*?(?!endpackage)*', a, re.MULTILINE|re.DOTALL))
这是输出:
['abc_y', 'abc_t', 'abc_x']
预期输出:
['abc_x']
我在正则表达式中遗漏了一些东西,但不知道是什么。有人可以帮我解决这个问题吗?提前致谢。

最佳答案


\bpackage.*?\bendpackage\b|typedef\s+struct\s+packed\s*{[^{}]*}\s*(\w+);
regex proof .
说明
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
package 'package'
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
endpackage 'endpackage'
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
typedef 'typedef'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
struct 'struct'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
packed 'packed'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
{ '{'
--------------------------------------------------------------------------------
[^{}]* any character except: '{', '}' (0 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
} '}'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
; ';'
Python code :
print(list(filter(None,re.findall(r'\bpackage.*?\bendpackage\b|typedef\s+struct\s+packed\s*{[^{}]*}\s*(\w+);', a, re.DOTALL))))
结果 : ['abc_x']

关于python - 获取 'package' 和 'endpackage' 可选字符串之外的结构名称列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67426732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com