c# - Regex for containing a word with specific characters -
i need regex find whether sentence contain word. if word having prefix or postfix word in valid valid specific characters ,.*+- etc below example. searching process word, valid sentence make them bold
- processes
- process
- processing
- process-specific
- processed
- processor
- procession
- inter-process
- uniprocessor
- multiprocessing
- process's
- "process"
- process10
use word-boundary class (\b
):
\bprocess\b
in case-insensitive regex match should give correct result:
string[] strings = new string[] { @"processes", @"process", @"processing", @"process-specific", @"processed", @"processor", @"procession", @"inter-process", @"uniprocessor", @"multiprocessing", @"process's", @"""process""", @"process10" }; foreach (string s in strings) { if (regex.ismatch(s, @"\bprocess\b", regexoptions.ignorecase)) console.foregroundcolor = consolecolor.green; else console.foregroundcolor = consolecolor.red; console.writeline(s); }
if trailing numbers allowed, do:
\bprocess(?:\b|\d+)
Comments
Post a Comment