You can use the RegEx. Replace method in VB. NET to search for a pattern in a string and modify the text to hide the information. The RegEx. Replace method is an efficient method for numerical replacement.
You can use a fixed string to replace a string. This increases the system load for developers to use it effectively.
In the example in list A, we look for a way to find a specific pattern in the string and replace it. You must provide a string to replace the text in the example, and use the common text XX/xxxx to find the birth date data in the string.
Code Description
We declare the string variable strtxt to save the original text value. At the same time, a RegEx variable rgxexp is defined to save the value of a fixed string text expression. Then, we assign the initial value of strtxt to the text with some data values, which is the information we intend to hide.
We use the RegEx. Replace method to replace all the Dob data with the specified expression. The result text is replaced with XX/xxxx in all the places where DOB appears, and the rest is the same as the original text. Then, the result string is displayed in a message box.
Code:
- Private sub replacepattern ()
- Dim strtxt asstring
- Dim rgxexp asnew system. Text. regularexpressions. RegEx ("d/D ")
- Strtxt = "Alison, DOB 01/01/1961; Bryan, DOB 12/13/1969"
- Strtxt = rgxexp. Replace (strtxt, "XX/xxxx ")
- MessageBox. Show (strtxt)
- End sub