Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, August 13, 2020 3:17 PM
Dear all,
I have a requirement to check if two words exist in a string.
I have written below code, but that is not working. For example, in the below code, I want to have both words Hi and hello existing in the string, "check". How to write regular expression to have both words Hi and hello any where in the string.
Please advise and thank you for your time and suggestion.
=============================================
Regex rx = new Regex(@"(Hi&hello)");
string check = "Hi and hello every one";
if (rx.IsMatch(check))
{
Console.WriteLine("string contains either Odoo or #rules");
}
==============================================
Narayana Reddy G
All replies (4)
Thursday, August 13, 2020 3:47 PM ✅Answered
Try another pattern:
Regex rx = new Regex( @"(?i)^(?=.\*\bhi\b)(?=.\*\bhello\b)" );
Thursday, August 13, 2020 5:10 PM ✅Answered
Thank you so much, it is working good. One last suggestion. If I would have to check the word followed by # symbol, how can the regular expression be?. Say I want to check, if the string contains both hi and #hello. How can the regular expression be in this case, I tried below code, but it does not work.
==========================================
Regex rx= new Regex(@"(?i)^(?=.*\bhi\b)(?=.*\b#hello\b)");
string check = "hi dfdfdfd and #hello";
if (rx.IsMatch(check))
{
Console.WriteLine("string contains both hi or #hello");
}==================================================
In this case remove '\b' before '#'.
Thursday, August 13, 2020 4:35 PM
Thank you so much, it is working good. One last suggestion. If I would have to check the word followed by # symbol, how can the regular expression be?. Say I want to check, if the string contains both hi and #hello. How can the regular expression be in this case, I tried below code, but it does not work.
==========================================
Regex rx= new Regex(@"(?i)^(?=.*\bhi\b)(?=.*\b#hello\b)");
string check = "hi dfdfdfd and #hello";
if (rx.IsMatch(check))
{
Console.WriteLine("string contains both hi or #hello");
}
==================================================
Narayana Reddy G
Thursday, August 13, 2020 7:27 PM
Thank you so much. It works like a charm!
Narayana Reddy G