[英]Escape special characters in a text when text is either enclosed in double quotes or not
我正在寫一個正則表達式來逃避一些特殊字符,包括輸入的雙引號。
輸入可以用雙引號括起來,那些不應該被轉義。
輸入結果:
"te(st", te(st, te"st
預期產出:
"te\(st", te\(st, te\"st
使用的代碼:
String regex = "^\".*\"$";
String value = "\"strin'g\"";
Pattern SPECIAL_REGEX_CHARS = Pattern.compile("[()'"\\[\\]*]");
if (Pattern.matches(regex, value)){
String val = value.substring(1, value.length() -1);
String replaceAll = SPECIAL_REGEX_CHARS.matcher(val).replaceAll("\\\\$0");
replaceAll = "\""+replaceAll+"\"";
System.out.println(replaceAll);
}else {
String replaceAll = SPECIAL_REGEX_CHARS.matcher(value).replaceAll("\\\\$0");
System.out.println(replaceAll);
}
1 - 檢查文本是否用雙引號括起來。 如果是,則轉義用雙引號括起來的文本中的特殊字符。
2 - 別的。 轉義文本中的特殊字符。
任何可以組合#1和#2的正則表達式?
此致,Anil
您可以使用if (s.startsWith("\\"") && s.endsWith("\\""))
來檢查字符串是否同時包含前導和尾隨"
,如果是,則可以刪除前導並跟蹤"
with replaceAll("^\\"|\\"$", "")
,然后使用轉義正則表達式轉義,然后添加"
返回。否則,只需轉義集合中的字符。
String SPECIAL_REGEX_CHARS = "[()'\"\\[\\]*]";
String s = "\"te(st\""; // => "te\(st"
String result;
if (s.startsWith("\"") && s.endsWith("\"")) {
result = "\"" + s.replaceAll("^\"|\"$", "").replaceAll(SPECIAL_REGEX_CHARS, "\\\\$0") + "\"";
}
else {
result = s.replaceAll(SPECIAL_REGEX_CHARS, "\\\\$0");
}
System.out.println(result.toString());
請參閱另一個IDEONE演示
appendReplacement
“回調”的替代解決方案 以下是使用替換的一個正則表達式的方法:
String SPECIAL_REGEX_CHARS = "[()'\"\\[\\]*]";
//String s = "\"te(st\""; // => "te\(st"
//String s = "te(st"; // => te\(st
String s = "te\"st"; // => te\"st
StringBuffer result = new StringBuffer();
Matcher m = Pattern.compile("(?s)\"(.*)\"|(.*)").matcher(s);
if (m.matches()) {
if (m.group(1) == null) { // we have no quotes around
m.appendReplacement(result, m.group(2).replaceAll(SPECIAL_REGEX_CHARS, "\\\\\\\\$0"));
}
else {
m.appendReplacement(result, "\"" + m.group(1).replaceAll(SPECIAL_REGEX_CHARS, "\\\\\\\\$0") + "\"");
}
}
m.appendTail(result);
System.out.println(result.toString());
請參閱IDEONE演示
要點:
Matcher#addReplacement()
與Matcher#appendTail()
允許操作組。 (?s)\\"(.*)\\"|(.*)
:2個選擇分支正則表達式".*"
匹配的字符串開頭"
和結尾"
(注意, (?s)
是一個DOTALL在線修改允許匹配字符串與換行符序列)或.*
替代匹配所有其他字符串。 "
兩端”。 \\\\\\\\\\\\\\\\
。 你可以使用負面的lookbehind和lookahead :
System.out.println(value.replaceAll("([()'\\[\\]*]|(?<!^)\"(?!$))", "\\\\$0"));
這基本上是說:在字符類[()'\\[\\]*]
轉義任何內容,或者"
不以字符串開頭或后跟字符串結尾的任何內容"
。
唯一的問題是,無論在另一端是否有相應的報價,都會忽略前導和尾隨報價。 如果這是一個問題,您可以鏈接這些替換以逃避不匹配的前導或尾隨引用:
.replaceAll("^\".*[^\"]$", "\\\\$0")
.replaceAll("(^[^\"].*)(\"$)", "$1\\\\$2")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.