gpt4 book ai didi

python - Python正则表达式来匹配和替换gradle依赖项输出

转载 作者:行者123 更新时间:2023-12-03 04:35:10 25 4
gpt4 key购买 nike

我是正则表达式和Python的 super 新手,但想实现这一目标。

我有这个输入:

\--- org.projectlombok:lombok:1.+ -> 1.16.6

+--- org.springframework:spring-aspects: -> 4.1.7.RELEASE
| \--- org.aspectj:aspectjweaver:1.8.6
+--- org.springframework.boot:spring-boot-starter-web: -> 1.2.6.RELEASE
| +--- org.springframework.boot:spring-boot-starter:1.2.6.RELEASE
| | +--- org.springframework.boot:spring-boot:1.2.6.RELEASE
| | | +--- org.springframework:spring-core:4.1.7.RELEASE
| | | \--- org.springframework:spring-context:4.1.7.RELEASE

需要获取Python脚本,正则表达式以删除行首的“\”,“-”,“+”和空格,并将“->”之前的单词或空格替换为“->”之后的单词或空格。

因此输出应为:
org.projectlombok:lombok:1.16.6
org.springframework:spring-aspects:4.1.7.RELEASE
org.aspectj:aspectjweaver:1.8.6
org.springframework.boot:spring-boot-starter-web:1.2.6.RELEASE
org.springframework.boot:spring-boot-starter:1.2.6.RELEASE
org.springframework.boot:spring-boot:1.2.6.RELEASE
org.springframework:spring-core:4.1.7.RELEASE
org.springframework:spring-context:4.1.7.RELEASE

顺便说一句,这是从gradlew依赖项目输出的。也许有一种方法可以从gradlew获得此输出?

好的,此代码在Python中完成:
#!/usr/bin/env python

import re

with open('testfile') as infile, open('testout', 'w') as outfile:
for line in infile:
line = line.replace("\\","")
line = line.replace("-","")
line = line.replace(" ","")
line = line.replace("+","")
line = line.replace("|","")
line = re.sub(r'(:[0-9.+]+)>', r':', line)
line = re.sub(r':>', r':', line)
outfile.write(line)

输出:
org.projectlombok:lombok:1.16.6

org.springframework:springaspects:4.1.7.RELEASE
org.aspectj:aspectjweaver:1.8.6
org.springframework.boot:springbootstarterweb:1.2.6.RELEASE
org.springframework.boot:springbootstarter:1.2.6.RELEASE
org.springframework.boot:springboot:1.2.6.RELEASE
org.springframework:springcore:4.1.7.RELEASE
org.springframework:springcontext:4.1.7.RELEASE

最佳答案

gradle是内置的依赖管理设计时,不确定为什么要使用python。一个简单的gradle任务可以为您编写文件。下面将通过运行gradle writeDependencyFile来执行

task writeDependencyFile << {
File output = new File("$project.rootDir.absolutePath/dependencies.txt")
configurations.compile.each { File file ->
output.append("${file.name.substring(0, file.name.lastIndexOf('.'))}\n")
}
}

关于python - Python正则表达式来匹配和替换gradle依赖项输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34786168/

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