开发者

Regex for removing part of a line if it is preceded by some word in Java

There's a properties language bundle file:

label.username=Username:
label.tooltip_html=Please enter your username.</center></html>
label.password=Password:
label.tooltip_html=Please enter your password.</center></html>

How to match all lines that have both "_html" and "</center></html>" in that order and replace them with the same line except the ending "</center></html>". For example, line:

开发者_如何学Python
label.tooltip_html=Please enter your username.</center></html>

should become:

label.tooltip_html=Please enter your username.

Note: I would like to do this replacement using an IDE (IntelliJ IDEA, Eclipse, NetBeans...)


Since you clarified that this regex is to be used in the IDE, I tested this in Eclipse and it works:

FIND:
(_html.*)</center></html>

REPLACE WITH:
$1

Make sure you turn on the Regular expressions switch in the Find/Replace dialog. This will match any string that contains _html.* (where the .* greedily matches any string not containing newlines), followed by </center></html>. It uses (…) brackets to capture what was matched into group 1, and $1 in the replacement substitutes in what group 1 captured.

This effectively removes </center></html> if that string is preceded by _html in that line.

If there can be multiple </center></html> in a line, and they are all to be removed if there's a _html_ to their left, then the regex will be more complicated, but it can be done in one regex with \G continuing anchor if absolutely need be.


Variations

Speaking more generally, you can also match things like this:

(delete)this part only(please)

This now creates 2 capturing groups. You can match strings with this pattern and replace with $1$2, and it will effectively delete this part only, but only if it's preceded by delete and followed by please. These subpatterns can be more complicated, of course.


if (line.contains("_html=")) {
   line = line.replace("</center></html>", "");
}

No regExp needed here ;) (edit) as long as all lines of the property file are well formed.


String s = "label.tooltip_html=Please enter your password.</center></html>";
Pattern p = Pattern.compile("(_html.*)</center></html>");
Matcher m = p.matcher(s);
System.out.println(m.replaceAll("$1"));


Try something like this:

Pattern p = Pattern.compile(".*(_html).*</center></html>");
Matcher m = p.matcher(input_line); // get a matcher object
String output = input_line;
if (m.matches()) {
  String output = input_line.replace("</center></html>", "");   
}


/^(.*)<\/center><\/html>/ 

finds you the

label.tooltip_html=Please enter your username. 

part. then you can just put the string together correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜