Reputation: 64642
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...)
Upvotes: 2
Views: 361
Reputation: 383956
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.
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.
Upvotes: 4
Reputation: 114817
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.
Upvotes: 3
Reputation: 15770
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>", "");
}
Upvotes: 1
Reputation: 59461
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"));
Upvotes: 1
Reputation: 2680
/^(.*)<\/center><\/html>/
finds you the
label.tooltip_html=Please enter your username.
part. then you can just put the string together correctly.
Upvotes: 0