Using Regular Expressions with VB.NET
At the moment, I am not planning a fully fleshed-out guided tour of VB.NET regex, although I certainly intend to add plenty of tasty material to this page over time. My pages are always in motion.
In the meantime, I don't want to leave you VB.NET coders out dry, so I have something special to get you started.
A VB.NET program that shows
how to perform common regex tasks
Whenever I start playing with the regex features of a new language, the thing I always miss the most is a complete working program that performs the most common regex tasks—and some not-so-common ones as well. This is what I have for you in the following complete VB.NET regex program. It's taken from my page about the best regex trick ever, and it performs the six most common regex tasks. The first four tasks answer the most common questions we use regex for:
✽ Does the string match?
✽ How many matches are there?
✽ What is the first match?
✽ What are all the matches?
The last two tasks perform two other common regex tasks:
✽ Replace all matches
✽ Split the string
If you study this code, you'll have a terrific starting point to start tweaking and testing with your own expressions with VB.NET. Bear in mind that the code inspects values captured in Group 1, so you'll have to tweak… but you'll have a solid base to understand how to do basic things&and fairly advanced ones as well.
As you can imagine, I am not fluent in all of the ten or so languages showcased on the site. This means that although the sample code works, a VB.NET pro might look at the code and see a more idiomatic way of testing an empty value or iterating a structure. If some idiomatic improvements jump out at you, please shoot me a comment.
Click to Show / Hide code
(The code compiles perfectly in VS2013, but no online demo supplied
because the VB.NET in ideone chokes on anonymous functions.)
(The code compiles perfectly in VS2013, but no online demo supplied
because the VB.NET in ideone chokes on anonymous functions.)
Read the explanation or jump to the article's Table of Contents
Smiles,
Rex