搜索不同的行并用正则表达式替换行尾

时间:2021-05-03 23:36:23

标签: regex

在Eclipse中查找,什么reg表达式? 输入 <CFCOOKIE name="JOE" value="2">
<CFCOOKIE name="Bill" value="2000">, etc...
在Eclipse中替换,什么reg表达式? 输出<CFCOOKIE name="Joe" value="2" httponly="yes" secure="Yes> <CFCOOKIE name="Bill" value="2000" httponly="yes" secure="Yes>", etc. 寻找 reg 表达式来完成这项工作。请帮助新手。这是在 Eclipse 中使用菜单——除了需要正则表达式之外没有其他语言。顺便说一句,原始数据可能有两个以上的属性。我需要在最后 >

之前插入 httponly="yes" secure="Yes"

1 个答案:

答案 0 :(得分:0)

首先。您没有指定语言。正如你是一个新手,你说

<块引用>

日蚀

我希望它是 Java,虽然我可能错了。

答案是使用某种 XML 解析函数来添加 属性并将它们恢复为字符串

或者如果你真的坚持使用正则表达式,你的模式看起来像这样:

"(<CFCOOKIE name=\")(\w*?)(\" value=\"\d+\")>"

使用它你会写:

String string = "<CFCOOKIE name=\"JOE\" value=\"2\">\n"
     + "<CFCOOKIE name=\"Bill\" value=\"2000\">";
String sub = "$1$2$3 httponly=\"yes\" secure=\"Yes\">";
        
Pattern pattern = Pattern.compile("(<CFCOOKIE name=\\\")(\\w*?)(\\\" value=\\\"\\d+\\\")>");
Matcher matcher = pattern.matcher(string);

String result = matcher.replaceAll(sub);
System.out.println("Result: " + result);

如果您必须将名称转换为 [A-Z][a-z] 形式,我建议您阅读有关如何做到这一点的内容并将 this answer 与我的结合起来。

正如您在评论中所述,您想使用 Eclipse 内置文本替换器。 要像您想要的那样替换,只需使用 <(CFCOOKIE .*?)> 并将其替换为 <$1 httponly="yes" secure="Yes"> 就可以了。

相关问题