简体   繁体   English

正则表达式(Java)查找所有以偶数个其他字符开头的字符

[英]Regex (Java) to find all characters preceded by an even number of other characters

I'd like to manipulate a String in Java using Regex. 我想使用Regex在Java中操作String。 Goal is to find all $ signs that have an even number of \\ in front of them (or none) and then add another \\ . 目标是找到所有$符号,在它们前面有偶数个\\ (或者没有),然后添加另一个\\

Example: 例:

"$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $"

should result in: 应该导致:

"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$"

Rationale here is: some $ are already escaped with a \\ and some escape \\ might be in the string as well in the form of \\\\. 这里的基本原理是:有些$已经使用\\进行了转义,而某些转义符也可能以字符串形式出现在字符串中。 I need to escape the remaining $. 我需要逃避剩下的$。

This should do the work: replace: 这应该做的工作:替换:

(^|[^\\])(\\{2})*(?=\$)

with the whole text matched (except for the lookahead), followed by \\\\ . 整个文本匹配(前瞻除外),后跟\\\\

Illustration in perl: 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

With Java, the whole text match is $0 . 使用Java,整个文本匹配为$0 Sample code: 示例代码:

// 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);
    }
}

Output: 输出:

"$ 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 \$"

A little explanation about the regex used is in order: 关于正则表达式的一点解释依次是:

                # 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

To be really complete, here are the positions at which the regex engine will find matching text (symbolized with <> ) and the cursor position (symbolized by | ): 要真正完成,下面是正则表达式引擎找到匹配文本(用<>符号表示)和光标位置(用|符号表示)的位置:

# 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

我觉得这样的事情可能有用:

\$(\\\\)*\\

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java&Regex:匹配不在特定字符前面的子字符串 - Java & Regex: Matching a substring that is not preceded by specific characters 如何使用Java正则表达式语法(不在Java代码中)查找一个单词,且该单词之前不包含100个字符的另一个单词 - How to find a word using java regex syntax (not in java code) that is not preceded with another word with in 100 characters 使用Java Regex匹配字母字符,且不带百分号 - Matching alphabetical characters with Java Regex which are not preceded by percent sign Java + Regex:匹配自定义集合中的字符,这些字符前面没有同一集合中的字符 - Java + Regex: matching characters from a customized set that are not preceded by characters in the same set java 正则表达式:如何找到前面没有空格和逗号的实数 - java Regex: how to find real number not preceded by spaces and comma Java RegEx:将所有xml字符替换为其实体编号 - Java RegEx: Replace all xml characters with their entity number Java正则表达式之间找到所有但最后一个字符没有前面或后面 - Java Regex Find All Between But Last Character Not Preceded Or Followed By 正则表达式检查前几个字符的偶数 - Regex to check even number of preceding characters 查找所有使用Java定义的正则表达式模式的允许字符 - Find all allowed characters defined a regex pattern using java 正则表达式以偶数个前置字符分割字符串 - Regex to Split String on Even Number of Preceding Characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM