Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 输入时匹配两个不同位置的正则表达式_Java_Regex - Fatal编程技术网

Java 输入时匹配两个不同位置的正则表达式

Java 输入时匹配两个不同位置的正则表达式,java,regex,Java,Regex,该用例用于重新格式化xml。我目前有一段代码如下所示: <dependencies> <dependency> <groupId> com.googlecode.java-diff-utils </groupId> <artifactId> diffutils </artifactId>

该用例用于重新格式化xml。我目前有一段代码如下所示:

<dependencies>
    <dependency>
        <groupId>
             com.googlecode.java-diff-utils
        </groupId>
        <artifactId>
             diffutils
        </artifactId>
        <version>
             1.3.0
        </version>
    </dependency>
</dependencies>
<dependencies>
    <dependency>
       <groupId>com.googlecode.java-diff-utils</groupId>
       <artifactId>diffutils</artifactId>
       <version>1.3.0</version>
    </dependency>
</dependencies>
output.replaceAll("<{TAG}>\\s+([^<>])\\s+</{TAG}>",  
                  "<{TAG}>($1)</{TAG}>")

com.googlecode.java-diff-utils
扩张器
1.3.0
我希望它看起来像这样:

<dependencies>
    <dependency>
        <groupId>
             com.googlecode.java-diff-utils
        </groupId>
        <artifactId>
             diffutils
        </artifactId>
        <version>
             1.3.0
        </version>
    </dependency>
</dependencies>
<dependencies>
    <dependency>
       <groupId>com.googlecode.java-diff-utils</groupId>
       <artifactId>diffutils</artifactId>
       <version>1.3.0</version>
    </dependency>
</dependencies>
output.replaceAll("<{TAG}>\\s+([^<>])\\s+</{TAG}>",  
                  "<{TAG}>($1)</{TAG}>")

com.googlecode.java-diff-utils
扩张器
1.3.0
因此,我希望匹配
对,这些对中没有其他对,如下所示:

<dependencies>
    <dependency>
        <groupId>
             com.googlecode.java-diff-utils
        </groupId>
        <artifactId>
             diffutils
        </artifactId>
        <version>
             1.3.0
        </version>
    </dependency>
</dependencies>
<dependencies>
    <dependency>
       <groupId>com.googlecode.java-diff-utils</groupId>
       <artifactId>diffutils</artifactId>
       <version>1.3.0</version>
    </dependency>
</dependencies>
output.replaceAll("<{TAG}>\\s+([^<>])\\s+</{TAG}>",  
                  "<{TAG}>($1)</{TAG}>")
output.replaceAll(“\\s+([^])\\s+”,
"($1)")

其中,
{TAG}
可以匹配。

正如其他人所说,您不应该使用正则表达式XML。使用XML解析器更容易、更健壮

然而,由于深夜正则表达式非常有趣,这里有一个简单的正则表达式:

String output = oldStr.replaceAll("(?m)<(\\w+)>\\s+([^<>]*)$\\s+</\\1>", "<$1>$2</$1>");
String output=oldStr.replaceAll(“(?m)\\s+([^]*)$\\s+”,“$2”);

同样,不要在prod代码中使用类似的东西。有很多边缘情况会破坏XML上几乎所有的正则表达式。

这里绝对应该使用XPath解析器,而不是正则表达式。搜索一下这个,你会找到你需要的。你有没有考虑过。。。我不知道。。。也许是xslt?另请参见,如果解析器包含属性,我肯定会使用它。。。但是,我是生成xml的人,因此我可以对它的外观有一定的保证。请尝试
\\b>\\R++\\h*+((?>\\s*[^\\s\\1
)。请参阅此处的实时演示是的,只需将相同的字符串指定给
replaceAll
方法。您如何知道第一个
(\\w+)
与第二个相同?哦,修复了它。尽管如此,由于两个标记之间都没有“”可以出现,我不确定是否有必要(因为这意味着在开始标记和结束标记之间不可能有另一个标记)…我必须再考虑一下。这是一个多行标志。它让我可以使用结束锚点(“$”)来匹配新行。