有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Regexp:在XML文件中替换版本号

好吧,我放弃了,我一直在尝试用ant编写一个regexp来替换属性文件中的版本号。我有以下资料:

<?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的版本或插件miriad的版本

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

考虑到“0.0.0”可以是任何数字,有没有简单的方法只匹配标签中的版本


共 (6) 个答案

  1. # 1 楼答案

    Perl替换正则表达式看起来像这样

    s/<feature(.*?)version=".*?"/<feature$1version="1.2.3.4"/s
    
  2. # 2 楼答案

    它可能有点重,但由于您处理的是XML,我建议您使用如下XSLT:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:template match="@*|*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*|*"/>
            </xsl:copy>
        </xsl:template>
    
    
        <xsl:template match="/feature/@version">
            <xsl:attribute name="version">
                <xsl:text>1.0.1</xsl:text>
            </xsl:attribute>
        </xsl:template>
    </xsl:stylesheet>
    
  3. # 3 楼答案

    假设您正在使用replaceregexp任务:

    <replaceregexp file="whatever"
                   match="(<feature\b[^<>]+?version=\")[^\"]+"
                   replace="\1${feature.version}" />
    

    我还假设只有一个<feature>元素

  4. # 4 楼答案

    只是为了好玩:一行,使用xpath(!),使用ruby

    (我知道:这不是一个regexp,但它是为了说明Will的建议,他说

    Now, since you're doing this with XML, the obvious question is, "Why aren't you using XPath?

    )

    键入“irb”,然后:
    需要“rexml/文档”;包括REXML;file=file。新的(“a.xml”);doc=文档。新文件;放置文档;博士。元素。每个(“/feature”){e | e.attributes[“version”]=“1.2.3”};放置文档

    它将所有“功能”元素的所有“版本”属性替换为“1.2.3”

    irb(main):001:0* require "rexml/document";include REXML;file = File.new("a.xml"); doc = Document.new(file);puts doc; doc.elements.each("/feature") {|e| e.attributes["version"]="1.2.3" }; puts doc
    
    <?xml version='1.0' encoding='UTF-8'?>
    <feature id='some.feature.id' version='1.0.0' provider-name='Provider' label='Some test feature'>
       <plugin unpack='false' id='test.plugin' download-size='0' version='0.0.0' install-size='0'/>
    </feature>
    
    
    <?xml version='1.0' encoding='UTF-8'?>
    <feature id='some.feature.id' version='1.2.3' provider-name='Provider' label='Some test feature'>
       <plugin unpack='false' id='test.plugin' download-size='0' version='0.0.0' install-size='0'/>
    </feature>
    
  5. # 5 楼答案

    那么:

    <feature[^<>]+version\s*=\s*"(\d+\.\d+\.\d+)"[^<>]*>
    

    括号中是与版本号匹配的组。移动括号以获得所需的精确匹配

  6. # 6 楼答案

    下面的正则表达式可以执行您想要的操作

    (?<=<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 lookbehinds的介绍ಠ_ಠ

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

    现在,既然您是使用XML来实现这一点的,那么一个明显的问题是,“为什么不使用XPath?”