regex - 如何使用 Notepad++ 解析有或没有声明版本的 Maven 依赖关系

标签 regex maven notepad++

我正在尝试解析 Maven 依赖项列表并以更简单的格式显示它。具体来说,我正在尝试转换以下标准 maven xml:

    <dependency>
        <groupId>groupId1</groupId>
        <artifactId>artifactId1</artifactId>
        <version>version1</version>
    </dependency>

对于这样的事情:

groupId1:artifactId1:version1

我正在使用的 poms 按 sortpom-maven-plugin 排序,因此我可以假设顺序始终为 groupId、artifactId、version;但我不能假设该版本将始终存在,或者不会有其他依赖项配置。

这意味着类似以下的事情是可能的:

    <dependency>
        <groupId>groupId1</groupId>
        <artifactId>artifactId1</artifactId>
        <version>version1</version>
    </dependency>
    <dependency>
        <groupId>groupId2</groupId>
        <artifactId>artifactId2</artifactId>
    </dependency>
    <dependency>
        <groupId>groupId3</groupId>
        <artifactId>artifactId3</artifactId>
        <version>version3</version>
    </dependency>
    <dependency>
        <groupId>groupId4</groupId>
        <artifactId>artifactId4</artifactId>
        <version>version4</version>
        <exclusions>
            <exclusion>
                <groupId>groupId4</groupId>
                <artifactId>artifactId4</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

我一直在使用 Notepad++ 尝试用正则表达式解析这些依赖项,我尽最大努力得出了以下结果:

搜索模式设置为正则表达式并选择匹配换行符时,我搜索以下内容:

.*?<dependency>.*?<groupId>(.*?)</groupId>.*?<artifactId>(.*?)</artifactId>.*?<version>(.*?)</version>.*?</dependency>.*?\R*

并替换为:

\1:\2:\3

不幸的是,这不包括省略版本的情况,因此以下

    <dependency>
        <groupId>groupId2</groupId>
        <artifactId>artifactId2</artifactId>
    </dependency>
    <dependency>
        <groupId>groupId3</groupId>
        <artifactId>artifactId3</artifactId>
        <version>version3</version>
    </dependency>

将转换为:

groupId1:artifactId1:version2

理想情况下,我希望看到它转换成这样的东西:

groupId1:artifactId1
groupId2:artifactId2:version2

有人对改进我的正则表达式有建议吗?它也不一定是单一的常规快车。如果运行顺序表达式最终会将依赖项转换为所需的格式,那就可以了。

最佳答案

第一次替换

查找内容:

<([^>]+)>([^<]+)</\1>(?:(?!\s*</)\s*)?

替换为:

\2:
  • 这将导致几乎您想要的结果:groupId1:artifactId1:version1:

第二次替换

要删除尾随的“:”,请查找内容:

:\s*?$

替换为:

{leave empty}

关于regex - 如何使用 Notepad++ 解析有或没有声明版本的 Maven 依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484697/

相关文章:

regex - Marpa 中不区分大小写的匹配

java - 如何使用 "%*%"作为分隔符在 Java 中拆分字符串,包括字符串结果列表中的分隔符?

java - 前瞻性思维 : Library Dependency handling in the workplace

java - 想要在导入到 Eclipse 的两个 GAE maven 项目之间共享公共(public)代码

regex - 在Notepad++中,在行尾添加每行的文本

java - 正则表达式处理零长度匹配

java - 如何为字符串编写正则表达式模式来识别空格或连字符之前的数字?

java - 项目在转换为 Maven 后从所有接口(interface)方法被覆盖的地方抛出错误

xsd - 在XML模式中定义maxLength构面的语法是什么?

regex - 记事本中的正则表达式或