Why Regex() may not work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why Regex() may not work?

I have var chekB = new Regex(@"\w+[k]\b"); and it can't find anything from sentence "I had to kill Frank. Crank". It was just a sentence. So why?

10th Sep 2019, 6:39 PM
Dead Lord
Dead Lord - avatar
11 Answers
+ 6
Dead Lord Add this to the top: ---- using System.Text.RegularExpressions; ---- Change your foreach to this: ---- foreach (var matchB in chekB.Matches(sent)) { Console.WriteLine(matchB); } ---- Remove or comment this line, because there isn't a console to read key from on SoloLearn: ---- Console.ReadKey(); ---- And remove the crude language in your code. It's just weird. BTW... If you review my code sample, you will see another option for doing this.
13th Sep 2019, 12:42 AM
David Carroll
David Carroll - avatar
+ 5
Send the link from Regex101.com and a link in Code Playground
11th Sep 2019, 3:23 AM
David Carroll
David Carroll - avatar
11th Sep 2019, 4:59 AM
David Carroll
David Carroll - avatar
+ 4
Groups refer to the sub-matches identified using the parentheses. Your RegEx pattern contains the grouping between the \b word boundaries.
13th Sep 2019, 3:32 PM
David Carroll
David Carroll - avatar
+ 2
It works on regex101.com
10th Sep 2019, 6:40 PM
Dead Lord
Dead Lord - avatar
+ 2
David Carroll ty. Later this night I will send it to you
11th Sep 2019, 4:10 PM
Dead Lord
Dead Lord - avatar
+ 1
Dead Lord Let me know if you have any questions on this.
11th Sep 2019, 3:41 PM
David Carroll
David Carroll - avatar
+ 1
David Carroll ok. Thank you!
13th Sep 2019, 8:06 AM
Dead Lord
Dead Lord - avatar
+ 1
Eah, that works. But can you tell me why does this works out? var chekA = new Regex(@"\b([a-z]{1,4})\b", RegexOptions.IgnoreCase); Console.WriteLine("a) Вывести только те слова, которые содержат не более 4х букв;"); foreach (Match matchA in chekA.Matches(sent)) { Console.WriteLine(matchA.Groups[1].Value); }
13th Sep 2019, 9:24 AM
Dead Lord
Dead Lord - avatar
+ 1
Actually I want to know why do I need write [1] here: matchA.Groups[1].Value
13th Sep 2019, 9:26 AM
Dead Lord
Dead Lord - avatar