正则表达式跳过模式 [英] Regexp skip pattern

查看:95
本文介绍了正则表达式跳过模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我需要将所有星号符号('*')替换为百分号('%').方括号中的星号符号应忽略.

I need to replace all asterisk symbols('*') with percent symbol('%'). The asterisk symbols in square brackets should be ignored.

示例

    [Test]
    public void Replace_all_asterisks_outside_the_square_brackets()
    {
        var input = "Hel[*o], w*rld!";
        var output = Regex.Replace(input, "What_pattern_should_be_there?", "%")

        Assert.AreEqual("Hel[*o], w%rld!", output));
    }

推荐答案

尝试使用前瞻性提示:

\*(?![^\[\]]*\])

这里有一个更强大的解决方案,它可以更好地处理 [] 块,甚至可以转义 \ [字符:

Here's a bit stronger solution, which takes care of [] blocks better, and even escaped \[ characters:

string text = @"h*H\[el[*o], w*rl\]d!";
string pattern = @"
\\.                 # Match an escaped character. (to skip over it)
|
\[                  # Match a character class 
    (?:\\.|[^\]])*  # which may also contain escaped characters (to skip over it)
\]
|
(?<Asterisk>\*)     # Match `*` and add it to a group.
";

text = Regex.Replace(text, pattern,
    match => match.Groups["Asterisk"].Success ? "%" : match.Value,
    RegexOptions.IgnorePatternWhitespace);

如果您不关心转义字符,可以将其简化为:

If you don't care about escaped characters you can simplify it to:

\[          # Skip a character class
    [^\]]*  # until the first ']'
\]
|
(?<Asterisk>\*)

可以这样写而没有注释: @"\ [[^ \]] * \] |(??< Asterisk> \ *)" .

Which can be written without comments as: @"\[[^\]]*\]|(?<Asterisk>\*)".

要了解其工作原理,我们需要了解Regex.Replace的工作原理:对于字符串中的每个位置,它都尝试匹配正则表达式.如果失败,它将移动一个 character .如果成功,它将遍及整个比赛.
在这里,我们为 [...] 块设置了虚拟匹配项,因此我们可以跳过不想替换的星号,仅匹配孤独的那些.该决定是在回调函数中做出的,该函数检查 Asterisk 是否匹配.

To understand why it works we need to understand how Regex.Replace works: for every position in the string it tries to match the regex. If it fails, it moves one character. If it succeeds, it moves over the whole match.
Here, we have dummy matches for the [...] blocks so we may skip over the asterisks we don't want to replace, and match only the lonely ones. That decision is made in a callback function that checks if Asterisk was matched or not.

这篇关于正则表达式跳过模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆