A regular expression to replace an entire string if it contains a specific word

I have a document with text, it contains some confidential information, such as NIC: 343434343. I need a regular expression that will do the following.

if it finds a NIC in a string, it should replace the entire string with the specified text.

+4
source share
3 answers

Since the default point does not match NewLine, you can simply use

.*NIC.* 

to find strings containing "NIC". You would use this expression, for example

 string result = Regex.Replace(originalString, ".*NIC.*", "replacement string"); 

You can see it at ideone.com .

+5
source

Use start and end line markers:

 ^.*NIC.*$ 

^ corresponds to the beginning of a line, and $ corresponds to the end of a line. This will match the entire string if it contains "NIC" at least once.

+2
source

Use this regular expression: (?mi)^.*?NIC.*$ . It enables the multi-line option and disables the option to ignore the option.

+1
source

All Articles