正则表达式,用于在Java中删除前有单词的行的一部分
有一个属性语言包文件:正则表达式,用于在Java中删除前有单词的行的一部分,java,regex,ide,Java,Regex,Ide,有一个属性语言包文件: label.username=Username: label.tooltip_html=Please enter your username.</center></html> label.password=Password: label.tooltip_html=Please enter your password.</center></html> 注意:我想使用IDE(IntelliJ IDEA、Eclipse、NetB
label.username=Username:
label.tooltip_html=Please enter your username.</center></html>
label.password=Password:
label.tooltip_html=Please enter your password.</center></html>
注意:我想使用IDE(IntelliJ IDEA、Eclipse、NetBeans…)来进行此替换{
行=行。替换(“,”);
}
此处不需要regExp;)(编辑)只要属性文件的所有行格式正确
部分。然后您可以将字符串正确地组合在一起。尝试以下方法:
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>", "");
}
(delete)this part only(please)
Pattern p=Pattern.compile(“.*”);
匹配器m=p.Matcher(输入线);//获取匹配器对象
字符串输出=输入线;
如果(m.matches()){
字符串输出=输入\行。替换(“,”);
}
String s=“label.tooltip\u html=请输入密码。”;
Pattern p=Pattern.compile((html.*);
匹配器m=匹配器p;
系统输出打印项次(m.replaceAll($1”);
既然您澄清了这个正则表达式将在IDE中使用,我在Eclipse中测试了它,它可以工作:
FIND:
(_html.*)</center></html>
REPLACE WITH:
$1
这将创建2个捕获组。您可以将字符串与此模式匹配,并替换为
$1$2
,它将有效地删除此部分,但前提是前面是删除
,后面是请
。当然,这些子模式可能更复杂。您使用的是什么编程语言?+1;使用正则表达式来简化不可避免的问题(失败|热破裂|溢出的啤酒)难道没有一些花言巧语吗?@amorfis:虽然这是明确的事实,但作为一个实际问题,它可能并不重要。但是,通过使用indexOf(String s)
检查子字符串的起始位置,就很容易修复了。
/^(.*)<\/center><\/html>/
label.tooltip_html=Please enter your username.
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>", "");
}
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"));
FIND:
(_html.*)</center></html>
REPLACE WITH:
$1
(delete)this part only(please)