tags:

views:

49

answers:

5

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:

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...)

+3  A: 
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.

Andreas_D
It won't work if "</center></html>" is before "_html".
amorfis
+1; aren't there some glib quotes about using regex to simplify problems inevitably ending in (failure|heatbreak|spilled beer)?
Carl
@amorfis: while that's explicitly true, as a practical concern it *probably* doesn't matter. Easy enough to fix by checking substring start positions with `indexOf(String s)`, however.
Carl
A: 
/^(.*)<\/center><\/html>/ 

finds you the

label.tooltip_html=Please enter your username. 

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

tarrasch
+1  A: 

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>", "");   
}
amorfis
+1  A: 
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"));
Amarghosh
+4  A: 

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.

polygenelubricants