开发者

Regular expression to replace whole line if it contains a particular word

开发者 https://www.devze.com 2023-03-23 07:21 出处:网络
I have a word document, it contains some confidential information lik开发者_C百科e it has NIC:343434343.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消