How to replace a pattern in a string using methods provided by VB. NET

Source: Internet
Author: User

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:
  1. Private sub replacepattern ()
  2. Dim strtxt asstring
  3. Dim rgxexp asnew system. Text. regularexpressions. RegEx ("d/D ")
  4. Strtxt = "Alison, DOB 01/01/1961; Bryan, DOB 12/13/1969"
  5. Strtxt = rgxexp. Replace (strtxt, "XX/xxxx ")
  6. MessageBox. Show (strtxt)
  7. End sub

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.