vb.Net - Regular Expression Tester

Reverend Jim 1 Tallied Votes 9K Views Share

vb.Net - Regular Expression Tester

Every now and then I find another use for a regular expression. For those not familiar with regular expressions, they can be as cryptic to read as strings of Greek letters. Simply put, regular expressions are just patterns. If you've ever used the DOS command shell dir command then you have likely used patterns, albeit simple ones. For example:

dir d:\temp\pic*.jpg

lists all files in the given folder that start with the letters pic and end with a .jpg extension. While the dir command allows only ? and * wildcards, regular expressions allow you to do so much more. They are frequently used to validate user input. There are patterns that match valid email addresses, social insurance numbers, phone numbers, etc. A common problem for beginners (or even professionals, for that matter) is figuring out what pattern to use. There are commercial programs, such as PowerGrep, available for testing regular expressions, but, being the cheap sort that I am, I decided to save a few bucks and just roll my own. It's not as flashy or feature rich as PowerGrep, or the tool that comes with Komodo Edit, but it is free. The code was written in vb.Net 2017. The entire project is attached as a zip file.

When you run it, you will see a minimal GUI consisting of a single-line textbox where you can type a pattern, two option boxes, and a multi-line rich textbox where you can enter (type or paste) text for testing. Feedback is real time. If you start by pasting one or more lines of text into the lower box, you will see what strings match the entered pattern, with matching string dynamically changing as you modify the pattern. Because the matches change as you enter the pattern, it may happen that the pattern will be in valid in mid-entry. For example, if you want to enter a character class such as

[0-9]

the pattern will become invalid as soon as you type [ and will not become valid until you enter ]. While the pattern is invalid it will be displayed in red and matching will stop. When the pattern becomes valid it will be displayed in black and dynamic matching will resume.

Pressing F1 at any time will cause a non-modal window to open displaying some brief notes about regular expression patterns. Please note that there is no absolute standard for regular expression syntax. This project, by necessity, follows the .Net core standard.

The two options are Multi-Line and Ignore Case. The first option controls how pattern matching handles end-of-line ($) markers and is best left checked for multi-line text. The second option is self-explanatory.

This is a first draft and will be modified as I add features or find problems. Constructive suggestions/feedback is always welcome.

An excellent resource for anyone wishing to learn more about regular expressions is "Beginning Regular Expressions" by Andrew Watt, ISBN: 0-7645-7489-2. Publised by Wrox Press/Wiley.

'please see attached project file
Reverend Jim 4,968 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Another useful tool is Regexper where you enter a regular expression and it displays it in railroad diagram style. For example, a Mastercard credit card number pattern

^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$

displays as

2018-07-16_164558.jpg

Reverend Jim 4,968 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Newer version also displays detailed information of matches, groups and captures in listview details format.

Reverend Jim 4,968 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Newer version posted. This version has two changes.

  1. Uses a splitter control for the lower two panels for easier sizing
  2. Does not report matches with a length of zero (faster display)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.