C# 正则表达式部分字符串匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/536147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:11:44  来源:igfitidea点击:

C# Regex partial string match

c#regex

提问by

everyone, i've got below function to return true if input is badword

大家,如果输入是坏词,我有下面的函数返回true

public bool isAdultKeyword(string input)
{
    if (input == null || input.Length == 0)
    {
        return false;
    }
    else
    {
        Regex regex = new Regex(@"\b(badword1|badword2|anotherbadword)\b");
        return regex.IsMatch(input);
    }
}

above function only matched to whole string i.e if input badword it wont match but it will when input is bawrod1.

上面的函数只匹配整个字符串,即如果输入坏词它不会匹配,但当输入是 bawrod1 时它会匹配。

what im trying to do it is get match when part of input contains one of the badwords

当输入的一部分包含坏词之一时,我试图做的是匹配

回答by cjk

So under your logic, would you match as to ass?

所以在你的逻辑下,你会像屁股一样匹配吗?

Also, remember the classic place Scunthorpe - your adult filter needs to be able to allow this word through.

另外,请记住经典的地方 Scunthorpe - 您的成人过滤器需要能够让这个词通过。

回答by renegadeMind

You probably don't have to do it in such a complex way but you can try to implement Knuth-Morris-Pratt. I had tried using it in one of my failed(totally my fault) OCR enhancer modules.

您可能不必以如此复杂的方式执行此操作,但您可以尝试实现Knuth-Morris-Pratt。我曾尝试在我失败的(完全是我的错)OCR 增强器模块之一中使用它。

回答by Richard Lennox

Is \b the word boundary in a regular expression?

\b 是正则表达式中的单词边界吗?

In that case your regular expression is only looking for entire words. Removing these will match any occurances of the badwords including where it has been included as part of a larger word.

在这种情况下,您的正则表达式只会查找整个单词。删除这些将匹配任何出现的坏词,包括它作为较大词的一部分被包括在内的地方。

Regex regex = new Regex(@"(bad|awful|worse)", RegexOptions.IgnoreCase);

回答by Mags

Try:

尝试:

Regex regex = new Regex(@"(\bbadword1\b|\bbadword2\b|\banotherbadword\b)"); 
return regex.IsMatch(input);

回答by Mike Clark

Your method seems to be working fine. Can you clarify what wrong with it? My tester program below shows it passing a number of tests with no failures.

你的方法似乎工作正常。你能澄清它有什么问题吗?下面我的测试程序显示它通过了许多测试而没有失败。

using System;
using System.Text.RegularExpressions;

namespace CSharpConsoleSandbox {
  class Program {
    public static bool isAdultKeyword(string input) {
      if (input == null || input.Length == 0) {
        return false;
      } else {
        Regex regex = new Regex(@"\b(badword1|badword2|anotherbadword)\b");
        return regex.IsMatch(input);
      }
    }

    private static void test(string input) {
      string matchMsg = "NO : ";
      if (isAdultKeyword(input)) {
        matchMsg = "YES: ";
      }
      Console.WriteLine(matchMsg + input);
    }

    static void Main(string[] args) {
      // These cases should match
      test("YES badword1");
      test("YES this input should match badword2 ok");
      test("YES this input should match anotherbadword. ok");

      // These cases should not match
      test("NO badword5");
      test("NO this input will not matchbadword1 ok");
    }
  }
}

Output:

输出:

YES: YES badword1
YES: YES this input should match badword2 ok
YES: YES this input should match anotherbadword. ok
NO : NO badword5
NO : NO this input will not matchbadword1 ok