[英]Java - Properly format a String given escape sequences and escape characters
Given new line (\\n), tab (\\t) and an escape character \\ how can I given a string format it properly so it deals with these escape sequences and escape characters properly. 给定换行符(\\ n),制表符(\\ t)和转义字符\\我如何给它正确设置字符串格式,以便它能正确处理这些转义序列和转义字符。 Example 1:
范例1:
"string \\t \t"
The output would be: 输出为:
"string \t "
So in this case \\\\t is escaped to just \\t and \\t is formatted with a tab 因此,在这种情况下,\\\\ t被转义为\\ t,而\\ t被制表符格式化
Example 2: 范例2:
"string \\t \n \\n"
The output is: 输出为:
"string \t
\n"
I tried brute-forcing a solution but it didn't work as I am having problems delimiting tabs and spaces with a backslash infront. 我尝试过强行解决方案,但由于我在用反斜杠开头来分隔制表符和空格时遇到问题,因此无法正常工作。
String v= "..." //v for value
v = v.replace("\\\"","\"");
v = v.replace("\\\\","\\");
v = v.replace("\\t", " ");
v = v.replace("\\n", "\n");
v = v.replace("\\\t", "\\t");
v = v.replace("\\\n", "\\n");
If I ran that code through the first example it would give: 如果我通过第一个示例运行该代码,它将给出:
"string "
looks like the one "brute force" combination you didn't try is correct 看起来像您没有尝试过的一种“强力”组合是正确的
replace("\\t", "\t")
reading "replace all < backslash >< t > combinations with < tab >" 阅读“用<选项卡>替换所有<反斜杠> <t>组合”
String them all together to get 将它们全部串在一起以获得
v = v.replace("\\t", "\t").replace("\\n", "\n")
(there's no need to replace \\ by itself) (无需单独替换\\)
You can first is replace escaped symbols and then replace escaped slashes with single slashes: 您可以首先是替换转义的符号,然后用单斜杠替换转义的斜杠:
[\, \, \, n] -> [\, \, \n] -> [\, \n]
We can do this by finding occurrences of two slash pairs: 我们可以通过找到两个斜杠对来实现:
(^|[^\\])(\\\\)*
- (^|[^\\]) is the start of the string or not a slash
- (\\\\)* is slash pairs
Combine this with the symbol you want to replace (for example \\n
): 将此与您要替换的符号结合起来(例如
\\n
):
((^|[^\\])(\\\\)*)(\\n)
Then we escape this string for java: 然后我们为Java转义此字符串:
((^|[^\\\\])(\\\\\\\\)*)(\\\\n)
Now you can write a helper method for this regex which keeps the first group $1
and replaces the second group: 现在,您可以为此正则表达式编写一个辅助方法,该方法保留第一组
$1
并替换第二组:
public static String replaceEscapedChar(
final String source,
final char escaped,
final char actual
) {
final String replacee = "(\\\\" + escaped + ")";
final String replacement = "$1" + actual;
return source.replaceAll("((^|[^\\\\])(\\\\\\\\)*)" + replacee, replacement);
}
For example. 例如。 The following produces:
产生以下内容:
replaceEscapedChar("Test\\\\\\nTest\\\\n", 'n', '\n');
Test\\
Test\\n
PS: You can also remove the quotes afterwards by writing: PS:您也可以通过以下方式删除引号:
source.replaceAll("((\\\\\\\\)+)", "\\\\");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.