RegEx
RegEx is a language used for string manipulation.
To me, RegEx has always been hard, but the alternatives are harder ;^).
My advise is, When you play with RegEx, do a lot of testing. Especially test strings with multiple copies of the expression you are looking for.
Replace with RegEx
1 | imports system.text.regularexpressions |
Replace has 3 arguments
- the string we want to play with
- the characters we want to find within the string
- the characters that will be converted
The first replace function above replaces 4 underscores (and a linefeed) with an <hr> tag.
The second replace function replaces plus B plus with a <b> tag.
The second and third arguments could contain regular expressions. Here are a few things to note.
- The backslash character is used by the expression function.
- \r means carriage return.
- \n means linefeed.
- The plus character is also used by the expression function. Since we need that we can prefix it with a \ character to indicate that’s a character we really want.
Another form of the replace function goes like this
1 | ParsedText = Regex.Replace(ParsedText, _ |
This will call a function called aFormTag which could resemble the following:
1 | Public Shared Function aFormTag(ByVal m As Match) As String |
\d{1,3} –> 1 to 3 digits