Regular expression to replace whole line if it contains a particular word
I have a word document, it contains some confidential information lik开发者_C百科e it has NIC:343434343. I need a regular expression which will do the following thing.
if it finds NIC on a line it should replace the whole line with specified text.
Since by default the dot does not match NewLine, you can simply use
.*NIC.*
to find lines containing "NIC". You'd use this expression like
string result = Regex.Replace(originalString, ".*NIC.*", "replacement string");
You can see it at work at ideone.com.
Use the start and end-of-line markers:
^.*NIC.*$
^
matches the start of line and $
matches the end of line. This will cause the entire line to be matched, if it contains "NIC" at least once.
Use this regex: (?m-i)^.*?NIC.*$
. It enables multiline option and disables ignore case option.
精彩评论