Regular Expressions: These tools help you create them easy

Regular expressions (or regex) are always used when searching for information that follows a basic pattern but can vary. Developers use regex, for example, to check valid input in forms. Regex is also a problem solver in databases, spreadsheets, or word processing when searching for patterns instead of factual content. This can be, for example, a tab-separated list of names sorted by first and last name, but which you would like to turn over.

Find the right expression

There are different “dialects” of regular expressions used by programming languages. The differences are marginal, so whoever has understood the principle will quickly recognize how to reformulate a phrase once it has been found. However, this already describes the main problem that many users have when they first encounter regex: finding the appropriate expression. If you are a beginner, it is worth visiting the English-language site , where a series of tutorials and hints are collected to familiarize yourself with the mechanics.

An up-and-coming project is the Service Regex Online Generator by Olaf Neumann. The way it works is straightforward. The tool helps you isolate the (probably) matching expression from a pattern. This limitation has to be insofar as the site naturally cannot know what you are looking for. To test this service, an actual data set was chosen: Reviewing the system logs of a Mac system revealed the sudden death of an application more frequently. As with Linux, the system log is very extensive and confusing. A regular printout is an excellent way to more quickly identify all the locations where the record was found.

So, for example, copy the error message, the searched terms, or a combination of numbers to the clipboard and paste the content into the first field of the web page. Your input will be analyzed. You will now see a whole series of colored markers in the second line. These are of different lengths and thus refer to other parts of the source material. What the tool thinks it has recognized appears as a hint when you point the mouse at one of the colored markers.

The explanations are in English but understandable. As soon as you select a pattern with a mouse click, the tool composes the regular expression based on it. This appears in the third line, “Regular expression.” There you will find two more essential radio buttons. Since the tool can’t know if you want to use your input as a pattern only or if you are looking for a combination of free search and the rest of the input, you can choose here.

“Generate only patterns” discards everything except the part you had selected with the mouse when generating the regular expression. Otherwise, all parts are copied.

“Match the whole line” additionally or optionally considers the position of the search pattern.

With “Copy regex,” the found expression ends up in the clipboard. If you need the expression in a unique dialect of a programming language, you might be happy with the lower area of the screen. Because there, the regex expression appears in the desired syntax.

Txt2regex as an alternative for the console

While the tool just presented already does a lot of work by grouping and analyzing the search expression, the second tool for regular expressions is simpler. But it belongs to the packages of most distributions and can be installed quickly. Txt-2regex runs in the terminal. The program, which you start via the program name directly on the console, uses the technique of a questionnaire to guide you step by step to the regular expression. To do this, it always displays several options or prompts you for input. Thus, in the first step, you must answer whether the desired expression may be located at the beginning of a line or any position. Once you have made your choice, it offers options. For example, are you looking for an arbitrary or a specific character? Or should it be a whole string (or word)?

You can also define a list of allowed characters. Depending on your input, the program then prompts you to store the string or the list of allowed characters. If you have not passed any other parameters when calling the program, txt2regex displays your regular expression at the top of the screen for use in various programs. While the software may seem old-fashioned at first glance (the first version was introduced 20 years ago), this does not diminish its usefulness.

The unique feature is that you can only answer the questions if you have already dealt with what you are looking for more intensively, i.e., have recognized the pattern behind it. The pleasant side effect: The more often you have worked with txt2regex, the more confident you will become in using regular expressions so that later you can manage without further aid.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store