当前位置: 动力学知识库 > 问答 > 编程问答 >

regex - c#: trying to use Regular Expressions to match a phrase

问题描述:

I trying to use a regular expression in C# to search for multi-word phrase. For example, I am currently pulling data data from a SQL Server table and I need to find a user-input keyword search "Hello World". This match could be at the beginning of a record, end of a record or anywhere in-between. I can match single word keyword wothout a problem.

Thanks in advance.

Found my answer: @"(\b)+" + strKeyword.txt + @"(\b)+";

At least it works!

网友答案:

For multi word matching use Match match = Regex.Match(text, @"(?:(hello world))") But to solve your problem follow the method @gabba has posted.

网友答案:
  1. If you use direct sql query use

    select * from table where field like '%Hello World%'

  2. If you use Linq use contains method

    var res = from rec in records where rec.field.Contains("Hello World") select rec

  3. If you have a collection with data you can match every element like this

    records.Where(x => System.Text.RegularExpressions.Regex.IsMatch(x.field, @"(?:(hello world))")).ToList();

  4. If you have string variable just use Regex:
    string variable = "bla bla Hello World woop";
    bool isExsistsReg = System.Text.RegularExpressions.Regex.IsMatch(variable, @"Hello World");

分享给朋友:
您可能感兴趣的文章:
随机阅读: