C#中/bad|words/gi的Regex是什么

本文关键字:Regex gi 是什么 bad words | 更新日期: 2023-09-27 18:07:08

https://regex101.com/r/oE4nG5/5

我想知道C#的Regex模式。我刚才链接的模式是针对Javascript的。/'bbad|'bwords/gi我想这样使用模式:https://dotnetfiddle.net/Widget/vQhJk9

using System;
using System.Text.RegularExpressions;
public class Program
{
    public static void Main()
    {
        string [] flame = {
            "bad", "words", "inc"
        };
        string argsInput = "some bad ";
        //Regex regex = new Regex(@"'bad|'words|'inc");
        var sjoin = string.Join("|''b", flame);
        Regex regex = new Regex ("@'"" + sjoin + "'"", RegexOptions.IgnoreCase);
        Console.WriteLine(sjoin);
        Match match = regex.Match(argsInput);
        if (match.Success) {
            Console.WriteLine("success");
        }
    }
}

C#中/bad|words/gi的Regex是什么

如下更改代码。

  • 您不需要匹配双引号,因为输入字符串不会包含任何双引号。"bad"表示bad是字符串而不是变量

代码:

string [] flame = {
    "bad", "words", "inc"
};
string argsInput = "some bad ";
//Regex regex = new Regex(@"'bad|'words|'inc");
var sjoin = string.Join("|''b", flame);
Regex regex = new Regex ( sjoin , RegexOptions.IgnoreCase);
Console.WriteLine(sjoin);
Match match = regex.Match(argsInput);
if (match.Success) {
    Console.WriteLine("success");
}

演示