我想使用Regex在Java中操作String。目标是找到所有$
个符号,前面有\
偶数(或没有),然后添加另一个\
。
示例:
"$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $"
应该导致:
"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$"
这里的基本原理是:某些$已经使用\进行了转义,而某些转义符也可能以字符串形式出现在\\中。我需要逃避剩余的$。
答案 0 :(得分:8)
这应该做的工作:替换:
(^|[^\\])(\\{2})*(?=\$)
整个文本匹配(前瞻除外),然后是\\
。
perl中的插图:
$ perl -pe 's,(^|[^\\])(\\{2})*(?=\$),$&\\,g'
"$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $" # in...
"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$" # out
"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$" # in...
"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$" # out
使用Java,整个文本匹配为$0
。示例代码:
// package declaration skipped
import java.util.regex.Pattern;
public final class TestMatch
{
private static final Pattern p
= Pattern.compile("(^|[^\\\\])(\\\\{2})*(?=\\$)");
public static void main(final String... args)
{
String input = "\"$ Find the $ to \\$ escape \\\\$ or not \\\\\\$ "
+ "escape \\\\\\\\$ like here $\"";
System.out.println(input);
// Apply a first time
input = p.matcher(input).replaceAll("$0\\\\");
System.out.println(input);
// Apply a second time: the input should not be altered
input = p.matcher(input).replaceAll("$0\\\\");
System.out.println(input);
System.exit(0);
}
}
输出:
"$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $"
"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$"
"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$"
关于正则表达式的一点解释依次是:
# begin regex:
( # start group
^ # find the beginning of input,
| # or
[^\\] # one character which is not the backslash
) # end group
# followed by
( # start group
\\{2} # exactly two backslashes
) # end group
* # zero or more times
# and at that position,
(?= # begin lookahead
\$ # find a $
) # end lookahead
# end regex
要真正完成,以下是正则表达式引擎将找到匹配文本(用<>
符号表示)和光标位置(由|
符号化)的位置:
# Before first run:
|"$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $"
# First match
"<>|$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $"
# Second match
"$ Find the <>|$ to \$ escape \\$ or not \\\$ escape \\\\$ like here $"
# Third match
"$ Find the $ to \$ escape <\\>|$ or not \\\$ escape \\\\$ like here $"
# Fourth match
"$ Find the $ to \$ escape \\$ or not \\\$ escape <\\\\>|$ like here $"
# From now on, there is no match
答案 1 :(得分:0)
我觉得这样的事情可能有用:
\$(\\\\)*\\