正则表达式在特殊字符之间获取字符串

时间:2017-02-25 06:57:33

标签: java regex

Iam尝试从字符串october中获取字符串["october"] 我尝试使用替换方法

 tagNo = tagNo.replaceAll("\\[|\\]", ""); \\ it prints "october"

我无法用""替换引号。

 tagNo = tagNo.replaceAll("\\[|\\]", "").replaceAll("\"", "\\\\\"");

不替换引号会打印\"october\"

但我更喜欢使用单一代码

使用regex代码如何获取带有quotessquare bracket的字符串

3 个答案:

答案 0 :(得分:2)

你需要稍微纠正你的正则表达式。您没有提出任何删除双引号的规则。试试这个,

tagNo = tagNo.replaceAll("(\\[|\"|\\])+", "");

答案 1 :(得分:1)

 tagNo = tagNo.replaceAll("[^a-zA-Z]+","");

答案 2 :(得分:0)

 tagNo = tagNo.replaceAll("\\[\"", "");
 tagNo = tagNo.replaceAll("\"\\]", "");

参考Java replace all square brackets in a string

Removing double quotes from a string in Java

相关问题