[英]What does the regex in this script's re.search() do?
I hope someone could help me with this code, I am looking forward to use it.我希望有人可以帮助我处理这段代码,我期待着使用它。
# Absolute path of the script:
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
# Real path of the script:
REAL_DIR = re.search(r'(?<=\[)(.*?)(?=\])',
subprocess.check_output('dir {} /al | findstr "<JUNCTION>" | findstr Ducati'.format(ROOT_DIR.replace("Ducati", "")), stderr=subprocess.STDOUT, shell=True).decode('ASCII')).group()
# Absolute path of the sandbox
SANDBOX_DIR = os.path.abspath(os.path.join(REAL_DIR, '..', '..'))
# Simple FlashTool Path
FLASHTOOL_DIR = os.path.abspath(
os.path.join(SANDBOX_DIR, 'DevelopmentEnvironmentPlatformTools', 'Tool.UDSFlashtool',
'build', 'INSTALL', 'flashtool', 'bin', 'simple-flashtool.exe'))
# Network config file
CONF_DIR_PATH = os.path.abspath(os.path.join(FLASHTOOL_DIR, '..', '..', 'conf'))
# Log file path
LOG_ROOT_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(ROOT_DIR))), 'tta_logs', 'FlashTool')
Especially this part尤其是这部分
# Real path of the script:
REAL_DIR = re.search(r'(?<=\[)(.*?)(?=\])',
subprocess.check_output('dir {} /al | findstr "<JUNCTION>" | findstr Ducati'.format(ROOT_DIR.replace("Ducati", "")), stderr=subprocess.STDOUT, shell=True).decode('ASCII')).group()
If you're asking specifically what the regex re.search(r'(?<=\[)(.*?)(?=\])'
does, it's finding strings that are surrounded by square braces and capturing the content inside the braces.如果您特别询问正则表达式re.search(r'(?<=\[)(.*?)(?=\])'
作用,它会查找被方括号包围的字符串并捕获其中的内容大括号。
(?<=\[)
is a positive lookbehind, asserting that before this match, there's a square opening brace [
(?<=\[)
是一个积极的向后看,断言在这场比赛之前,有一个方括号[
(.*?)
is a capture group that selects any characters any number of times, but the ?
(.*?)
是一个捕获组,可以多次选择任何字符,但是?
quantifier means it captures as few as possible.量词意味着它尽可能少地捕获。 This means it won't usually capture the closing square brackets ahead of it;这意味着它通常不会捕获它前面的右方括号; if you've got a[s]df[ghj]kl
, it'll capture s
and ghj
instead of s]df[ghj
.如果您有a[s]df[ghj]kl
,它将捕获s
和ghj
而不是s]df[ghj
。(?=\])
is a positive lookahead, asserting that after this match, there's a square closing brace ]
(?=\])
是一个积极的前瞻,断言在这场比赛之后,有一个方括号]
See it in action here!在这里看到它的行动!
I can sort of guess at the rest of the script, which looks like it's searching for symbolic links ("shortcuts") to something.我可以猜测脚本的 rest,它看起来像是在搜索某个东西的符号链接(“快捷方式”)。 The presence of dir {} /al
makes me think this script is supposed to be run on Windows. dir {} /al
的存在让我觉得这个脚本应该在 Windows 上运行。 subprocess.check_output()
is going to run the command string inside of it, and then pipe its (possibly multiline) output to the regex, which is then going to use .group()
to create a tuple of the matches it finds. subprocess.check_output()
将在其中运行命令字符串,然后 pipe 其(可能是多行) output 到正则表达式,然后将使用.group()
创建它找到的匹配项的元组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.