正则表达式怎么才能不匹配到指定的字符集

.Net技术 码拜 8年前 (2016-02-23) 779次浏览
例如本人想匹配单词:
正则表达式怎么才能不匹配到指定的字符集
但又想不匹配到关键字,例如” function”, “end”等单词:

and|break|do|else|elseif|end|false|for|function|if|in|local|nil|not|or|repeat|return|then|true|until|while

本人想问下这种可以单独通过正则表达式解决吗?

解决方案

1

用 ^ 非吗!

9

@"(?!and|break|do|else|elseif|end|false|for|function|if|in|local|nil|not|or|repeat|return|then|true|until|while)\b\w+\b"

10

应该是”零宽度负预测先行断言”。

var s = "function abc(string s)";
var matches = Regex.Matches(s, "(?<word>(?!function|string)\b\w+)");
foreach (Match m in matches)
{
    if (m.Success)
    {
        Console.WriteLine(m.Groups["word"].Value);
    }
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明正则表达式怎么才能不匹配到指定的字符集
喜欢 (0)
[1034331897@qq.com]
分享 (0)