top of page
Search

De-mystifying Regex for QA Automation

  • Writer: Kimberly Coleman
    Kimberly Coleman
  • Apr 1, 2024
  • 3 min read

Updated: Apr 3, 2024

ree

Starting out as a baby QA Analyst trying to introduce automation can be a daunting task. Especially if the feature or project you're working on was not conceived with automation in mind. When QA is not involved early in the development process it becomes increasingly difficult to implement automation solutions and sometimes even identify them in the first place! It’s a bit like trying to make the container for something after it’s already been opened and played with; some stuff is inevitably going to stick out the sides a little.


When software is perhaps older, or not planned with automation in mind, the bits are often not arranged neatly enough for perfect textbook automation solution. It takes experience, industry know-how, and a bit of elbow grease to create a satisfactory result. One of the tools I’ve found invaluable is the humble Regular Expression.


As a basic refresher: Regular expressions (Regex) are logic patterns used to match character combinations in strings. They can make verifying a complicated text pattern a breeze. However, they seem intimidating (At least to me when I was starting out) and more like a random bit of gibberish. In this article, I give some tips on how to use regex and share some of the ways I’ve used them in my job.


QA professionals can make use of Regex in these main areas:


  1. Validating data: When an application receives some text input, running it through a quick regex to sanitize and format it can help avoid processing errors and ensure that the data is being received like you expect. Things like emails, addresses, and urls all have predefined formats that are expected to appear a certain way to applications. That’s how input forms know if you entered your email right.

  2. Extracting information: Sometimes you only need certain kinds of information from a large bit of data. Regex makes it easy to search for specific patterns to extract for use later such as tokens or ids.

  3. Replacing data: Related to extracting; once you’ve found the data pattern you’re looking for, you can replace it with something else. This is helpful when transforming large datasets.


Syntax

Regex follows a logic pattern with a long list of constraints. (I personally have to look up a cheat sheet all the time 😅) But here are some of the basic ones that will get you through most applications:


Character Classes

[abc]	Match any of a, b, and c.
[a-z]	Match any character between a and z.
[^abc]	Match any OTHER THAN a, b, and c.
.		Match any single character except line terminators.
\d		Match any digit (0-9).
\D		Match any character that is not a digit (0-9)
\w		Match any character from the basic Latin alphabet.

Boundaries

^ 		Match the beginning of input.
$		Match the end of input.
\b		Match a character at the beginning or end of a single word.

Lookaheads/Lookbehinds

x(?=y)	Match "x" only if "x" is followed by "y".
x(?!y)	Match "x" only if "x" is NOT followed by "y".
(?<=y)x	Match "x" only if "x" is preceded by "y".
(?<!y)x	Match "x" only if "x" is NOT preceded by "y".

There are loads more syntax you can use and regex can get very long and complicated indeed. Luckily there are some online resources that can help you find the regex you need such as Regex101 and MDN Web Docs.



Personal Examples

Finally, let’s look at some of the regex that I’ve used. It's in JavaScript, but regex can be used in most programming languages. Don't laugh, these did the trick haha 😂.


.replace(/[^0-9\.]+/, '')

My bread and butter regex that replaces any character that is not a number with nothing. This can be expanded upon to include special characters like a literal '.' but it's not always necessary.


.replace(/(\d{1,2})\/(\d{1,2})\/(\d{4})/, "$3-$1-$2")

 This regex helped me tackle my eternal foe in test automation: Formatting dates. It searches for sets of 1 or 2 digits twice and then 4 digits separated by the '/' character, representing a MM/DD/YYYY date. Then it's plugged into a replace method to reorder the segments into YYYY/MM/DD instead.



Comments


Address

Straffordville, ON

Phone

226-377-0899

Email

Connect

  • LinkedIn
bottom of page