我的问题是,我试图写一个正则表达式键来找到某些行。这里是数据
| |-UsingDirectiveDecl 0x16de688 <line:58:3, col:24> col:24 Namespace 0x16de588 '__debug'
|-UsingDirectiveDecl 0x1e840b8 <simple.cpp:2:1, col:17> col:17 Namespace 0x1378e98 'std'
这里是正则表达式代码
import subprocess as subs
import os
import re
file = "simple.cpp"
full_ast = subs.run(["clang -Xclang -ast-dump %s" % file], shell=True, stdout=subs.PIPE) # , (" > %s.xml" % title)
namespace_s = re.search("UsingDirectiveDecls0x([+-]?(?=.d|d)(?:d+)?(?:.?d*))(?:[eE]([+-]?d+))?s<simple.cpp:[0-9]+:[0-9]+,s[a-zA-Z]+][0-9]+>s[a-zA-Z]+:[0-9]+sNamespaces0x([+-]?(?=.d|d)(?:d+)?(?:.?d*))(?:[eE]([+-]?d+))?s'[^']*'", str(full_ast.stdout))
# not sure if re.search is the right module.
print(namespace_s)
我在努力达到底线,只有我成功了。我想发生的两件事,1:那里有一个像0x1e840b8的偏移量,我需要它匹配为0x7hexcharacters -最初我尝试0x[a-z0-9]{7},但没有工作。2:我怎么把文件名放进去,它会与%s一起工作,然后与% file
连接键吗?任何帮助都是非常感谢的
关于正则表达式,您正在尝试将(?:d+)?(?:.?d*))(?:[eE]([+-]?d+))?
与十六进制部分匹配,但您可以使用0x[a-f0-9]{7}
代替。
如果你是匹配的,你不需要向前看(?=.d|d)
还有一个额外的右括号]
,不在示例数据中,应该是:
<simple.cpp:[0-9]+:[0-9]+,s[a-zA-Z]+]
^
请看下面的例子:
UsingDirectiveDecls0x[a-f0-9]{7}s+<simple.cpp:[0-9]+:[0-9]+,s[a-zA-Z]+:[0-9]+>s[a-zA-Z]+:[0-9]+sNamespaces0x[a-f0-9]{7}s'[^']*'
Regex demo | Python demo
例子import re
pattern = r"UsingDirectiveDecls0x[a-f0-9]{7}s+<simple.cpp:[0-9]+:[0-9]+,s[a-zA-Z]+:[0-9]+>s[a-zA-Z]+:[0-9]+sNamespaces0x[a-f0-9]{7}s'[^']*'"
s = ("test | |-UsingDirectiveDecl 0x16de688 <line:58:3, col:24> col:24 Namespace 0x16de588 '__debug' testn"
"test |-UsingDirectiveDecl 0x1e840b8 <simple.cpp:2:1, col:17> col:17 Namespace 0x1378e98 'std' test")
print(re.findall(pattern, s))
输出["UsingDirectiveDecl 0x1e840b8 <simple.cpp:2:1, col:17> col:17 Namespace 0x1378e98 'std'"]