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.
If you use direct sql query use
select * from table where field like '%Hello World%'
If you use Linq use contains method
var res = from rec in records where rec.field.Contains("Hello World") select rec
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();
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");