简体   繁体   English

Java正则表达式模式仅匹配字符串的最后一次出现

[英]Java regex pattern to match only the last ocurrence of string

In a html doc, I need to replace the full path to files with just the file names. 在html doc中,我需要仅用文件名替换文件的完整路径。 The documents are very large so I think I can use regex to obtain a practical solution. 这些文档非常大,因此我认为我可以使用正则表达式来获得实用的解决方案。 I've already read similar questions and tried the solutions but that just did'nt work. 我已经阅读过类似的问题,并尝试过解决方案,但是那没有用。

Example. 例。 Given this html input. 给定此html输入。

<img src="app/javax.faces.resource/color_pan.png?ln=img/partidos" style="width:100%; height:30px;" class="centerImg"/>
<img src="/app/javax.faces.resource/pan.png?ln=img/partidos" class="centerImg"/>

I need the folowing output: 我需要以下输出:

<img src="color_pan.png" style="width:100%; height:30px"; class="centerImg"/>
<img src="pan.png" class="centerImg"/>

I'm trying these patterns: 我正在尝试以下模式:

Pattern p = Pattern.compile("src=\"(?=.*src).*/color_pan.png[^\"]*\"");
Patter p1 = Pattern.compile("src=\"(?!.*src).*/pan.png[^\"]*\"");

The first one works fine for the 1st image and the second one is the solution for the 2nd (both are on the same html doc). 第一个适用于第一个图像,第二个适用于第二个图像(都在同一个html文档上)。 I need a general pattern that works for every image. 我需要一个适用于每个图像的通用模式。 So the problem is to find only the first "src" element that appears left to the file name. 因此,问题在于仅找到出现在文件名左侧的第一个“ src”元素。 In other words, the "src" must be the last one that appears before the file name. 换句话说,“ src”必须是出现在文件名之前的最后一个。 That way, I could replace the strings correctly. 这样,我可以正确替换字符串。 Any help is appreciated. 任何帮助表示赞赏。

This regex seems to do the work 这个正则表达式似乎可以完成工作

Solution 1 <= 2 matches in 1509 steps 解决方案1 ​​<= 2符合1509步

(^<img src=")(?:.*?)([\w.]+)(?=\?)[^"]*"(.*$)

Regex Demo 正则表达式演示

Towards an efficient solution 寻求有效的解决方案

Solution 2 <= 2 matches in 593 steps 解决方案2 <= 2符合593步骤

(^<img src=").*(?<=\/|")([\w.]+)(?=\?)[^"]*"(.*$)

Java Code Java代码

String pattern = "(^<img src=\")(?:.*?)([\\w.]+)(?=\\?)[^\"]*\"(.*$)";
Pattern r = Pattern.compile(pattern);

while (true) {
     String line = x.nextLine();
     Matcher m = r.matcher(line);
     if (m.find()) {
         System.out.println(m.group(1) + m.group(2) + m.group(3));
     } else {
         System.out.println("Not Found");
     }
}

Ideone Demo Ideone演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM