From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Regex Helper

Code and Documents

Attachment

   File I/O Category Winner


Overview

We all know that regular expressions (regexes) are extremely powerful for many string operations, including parsing, cleaning, and rearranging.  For most of us, however, reading a regex is akin to hieroglyphics, and who really wants to memorize all of those special codes and constructs.  This tool allows you to construct and test a regular expression using pulldown menus when you need a reminder of what all of the special codes mean.  When you have developed that killer regex, save it as a macro and it will be just a couple of clicks away whenever you need it.

Description

This Tool allows you to develop and test Perl Compatible Regular Expressions (PCRE) used by Match Regular Expression and Search and Replace.  They are extremely powerful and flexible, but potentially confusing and frustrating.  With this Tool you can load an input string from a file, a URL, or by typing/pasting into the Input string control.  Next you can build your regex by hand, with a little help from the pulldown menus when you have forgotten what code to use.  When you are ready, press Test or First and you will see the whole match in bold, with underlined and colored submatches.  You can use the Prev and Next buttons to find other matches in the Input string.

On the Macros Tab you can add or delete Macros which are stored in a configuration file.  I have included a couple of examples, when you understand the Match Parentheses macro, you are on your way to regex ninja-hood.

The Help tab contains a summary of the syntax from the PCRE website.  It is searchable as well.

Steps to Implement or Execute Code

  1. Unzip the attached code
  2. Open RegexHelper.vi  (it is set to run when opened)
  3. Choose the input string:
    1. Type/paste into string control
    2. Type/paste URL into URL control (great for parsing html from a website)
    3. Drag a file onto the front panel (the background is a drop target via a transparent Path control)
    4. Click the Browse button (the folder) to select a local file
  4. Build your regex.  Clicking the '+' buttons will add the currently selected expression to the regex, and in most cases move the insertion point to a natural location.  (Thanks NI for the + button from the icon editor)
  5. Click 'Test' or 'First' to start testing.  The whole match is bold, submatches are underlined and alternate between red and blue.  This is modifiable when you peek under the hood.  Hint:  when I am matching space or whitespace, I add a capture group () so the match is underlined.  Some characters do not bold as well, but overall I prefer this behavior
  6. When finished, press 'Stop'.  Now you can view the code and modify at will

I have added this tool to my Tools menu, you can follow the instructions on the block diagram to do so as well.  Change the VI to Run when opened, enable the Front Panel Close code in the disable structure, then copy the code to the project folder as shown.  The SubVIs are located in a folder which starts with an underscore so they will not appear in the Tools menu.

Requirements to Run

Software

LabVIEW 2009 or later

Hardware

None

Additional Images

RegexHelperScreenshot3.png

Most of the code is fairly straightforward with your typical While Loop/Event Structure to handle the UI.  There is one potential surprise lurking when you examine the code which tests the regex, you would probably expect to find a 'Match Regular Expression' function in there somewhere, and you will not (I use it for searching the help).  This function is implemented as an XNode, and inside vi.lib are two VIs which serve as the business end of this XNode.  They are contained in the two snippets below and I often use them directly.

RegexHelperSnippet.pngRegexHelperSnippet2.png

Other things to note in the code:

I have implemented Ctrl-a (Select All) behavior in the Input String and Regex controls.

The background of the front panel is a transparent classic path control, this allows the panel to act as a drop target for files without calls to Windows API, or other platform-specific functions.

V2 : Fixed Property Nodes, Help Search is case insensitive, coerce issue with LV10.

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
stbe
Active Participant
Active Participant
on

"Content-Type Not allowed errors": rename your images with lower case file extensions -> then it's working

_________________________
CLA
Darin.K
Trusted Enthusiast
Trusted Enthusiast
on

Thanks, that did the trick.

altenbach
Knight of NI Knight of NI
Knight of NI
on

Darin, when loading in 2010f2, I get the following load warning for RegexHelper_SetStringPosition.vi:

(RegexHelper_SetStringPosition.vi)

    - An In Range & Coerce function has both signed and unsigned integers wired.  Previously all inputs were incorrectly treated as signed; now mixed inputs will be coerced to the output type. Please wire consistent types to ensure intended behavior.

While it probably has no ill effects here, You should make sure to fix the coercion. Nobody wants to see confusing (to some!) popups when loading a released VI.

RegexWarning.png

>> "Drag a file onto the Front Panel (the background is a drop target via a transparent Path control)"


This seems to have the unwelcome side-effect that clicking anywhere on the FP background (=actually the path control) during an editing session, turns the entire FP black and it is not immediately obvious how to get things back to normal. Is there a way to avoid this?

altenbach
Knight of NI Knight of NI
Knight of NI
on

Looks Great! A few minor code simplification suggestions:

In the event cases:

  • "Special Codes": Value change"
  • "Assertions and conditionals": Value change
  • "Quantifiers": Value change

... there are identical property nodes that are used in all cases of a small case structure. These belong after the case structure as a single instance. The current situation creates e.g. problems when searching for a certain property node because there will be an excessive amount of matches, possibly slowing down the programmer.

Darin.K
Trusted Enthusiast
Trusted Enthusiast
on

Thanks for the heads up, I'll check out the LV10 issue,   Good catch on the PNs, I rearranged the menus to simplify the UI and did not manage to simplify the code as well.  Fixed in next version (posting later today I hope). 

I added a visibility toggle to the hidden path control which hides it when the VI is stopped.  Now you can click at will in the FP.  I use an Event Case to stop clicks when it is running.

AELmx
Member
Member
on

Mr. Darin

Is there any possibility to get the presentation about regex from 2016 NI week? Any material from that day would be great.
Its not available on NI app.

AndreasStark
NI Employee (retired)
on

I also found the tool expresso extremely helpful in developing regex like a pro: http://www.ultrapico.com/expresso.htm

Andreas Stark
LabVIEW Lead @ Rocket Factory Augsburg
Contributors