gpt4 book ai didi

java - 正则表达式 : Substituting a version number in an XML file

转载 作者:行者123 更新时间:2023-11-29 06:48:11 32 4
gpt4 key购买 nike

好吧,我放弃了,我一直在尝试用 ant 编写一个正则表达式来替换我在属性文件中的版本号。我有以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<feature
id="some.feature.id"
label="Some test feature"
version="1.0.0"
provider-name="Provider">

<plugin
id="test.plugin"
download-size="0"
install-size="0"
version="0.0.0"
unpack="false"/>
..... many plugins later....
</feature>

我想要实现的是只替换功能标签的版本号,而不更改文件中的 xml 版本或无数插件的版本。

我遇到的问题是我要么匹配太多要么太少。明确匹配“版本”是不够的,因为一切都会改变

考虑到“0.0.0”可以是任何数字,是否有任何简单的方法可以仅匹配标签内的版本?

最佳答案

下面的正则表达式可能会做你想做的......

(?<=<feature[^>]{*,1000}version=")[^"]*(?=")

用人类的话来说,大致意思是

I want to match text that immediately follows the string "<feature", followed by up to a thousand characters not the greater-than bracket, and "version=""; the text to match is immediately followed by a double quote, and contains no double quotes.

**感谢 Alan M 提出有关 java 后视的提示 ಠ_ಠ

如果您将此正则表达式与替换操作一起使用,它会将 version=""属性中的任何内容替换为您设置为替换值的任何内容。由您提供格式正确的版本号。

现在,由于您正在使用 XML 执行此操作,因此显而易见的问题是,“您为什么不使用 XPath?”

关于java - 正则表达式 : Substituting a version number in an XML file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/182519/

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