Blog
Wild & Free Tools

Regex for QA Testers — Validate Form Inputs and Test Patterns Faster

Last updated: April 2026 7 min read

Table of Contents

  1. Reading a Dev's Validation Regex
  2. Test Case Generation from Regex
  3. Regex in Selenium and Playwright Tests
  4. Testing API Response Fields
  5. Building a Test Matrix from Regex
  6. Frequently Asked Questions

As a QA tester or SDET, regex serves two purposes: understanding the validation rules the dev team implemented (so you can write comprehensive test cases), and testing regex patterns yourself when you're writing automated tests with Selenium, Playwright, Cypress, or pytest.

This guide covers both angles — how to reverse-engineer validation rules from regex, how to generate test cases that break validators, and how to write regex in your own test code.

How to Read a Developer's Validation Regex and Write Test Cases From It

When a dev hands you a regex for field validation, the browser tester lets you understand exactly what it accepts and rejects — without asking them to explain it.

Step by step:

  1. Paste the pattern into the tester
  2. Test obvious valid values — does a normal email address match? A standard phone number? A typical password?
  3. Find the boundaries — what is the minimum and maximum length? What characters are on the edge of acceptance?
  4. Test boundary conditions — one character under the minimum, one over the maximum, first and last valid character in a range
  5. Test invalid patterns — empty string, spaces only, special characters the pattern seems to reject
  6. Document what you found — this becomes your equivalence partitioning and boundary value analysis test matrix

For example, if the email regex is ^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$, you can immediately see: no spaces allowed, the + symbol is allowed, the TLD must be at least 2 characters. Write test cases for all of these.

Generating Test Cases from Validation Regex — A Systematic Approach

For any validation regex, generate at least these test categories:

Valid partition (should pass):

Invalid partition (should fail):

Edge cases:

Paste each test value into the browser tester and verify the match result matches what you expect the application to do. Inconsistencies reveal bugs in the validation rule.

Sell Custom Apparel — We Handle Printing & Free Shipping

Writing Regex in Selenium, Playwright, and Cypress Tests

Playwright (Python):

import re
# Assert that the text matches a pattern
expect(page.locator('.price')).to_have_text(re.compile(r'\$[0-9,]+\.\d{2}'))
# Wait for a URL matching a pattern
page.wait_for_url(re.compile(r'/checkout/success/\d+'))

Playwright (JavaScript/TypeScript):

// Assert element text matches pattern
await expect(page.locator('.status')).toHaveText(/Order #\d{6} confirmed/);
// Navigate and check URL
await expect(page).toHaveURL(/\/user\/\d+\/profile/);

Pytest with regex assertions:

import re
assert re.match(r'^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$', order_id)

Test all of these patterns in the browser tester with representative values before adding them to test code. A regex assertion that always passes because the pattern is wrong will silently let bugs through your test suite.

Using Regex to Validate API Response Fields

API tests often need to assert that response fields match expected formats without specifying exact values. Regex is the right tool:

Validate an order ID format: ^ORD-\d{6}$
Test with: ORD-123456 (pass), ORD-12345 (fail — too short), ORD-ABCDEF (fail — not digits)

Validate a timestamp format: ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z?$

Validate a currency amount: ^\d+\.\d{2}$
Test: 12.99 (pass), 12.9 (fail), 12 (fail)

Validate a status code value: ^(pending|processing|shipped|delivered|cancelled)$

In Postman, these go in the Tests tab:
pm.test("Order ID format", () => { pm.expect(pm.response.json().orderId).to.match(/^ORD-\d{6}$/); });

In REST Assured (Java):
.body("orderId", matchesPattern("^ORD-\d{6}$"))

Building a Test Matrix from a Validation Regex

When a developer sends you this password validation regex: ^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

Here is how to build a complete test matrix:

Test CaseInputExpectedWhy
All rules metPassword1!PassBaseline valid
No uppercasepassword1!FailMissing [A-Z]
No lowercasePASSWORD1!FailMissing [a-z]
No digitPassword!!FailMissing \d
No special charPassword11FailMissing [@$!%*?&]
Too short (7 chars)Pass1!AFailBelow {8,} minimum
Exactly 8 charsPass1!AbPassAt minimum length
Unlisted special charPassword1#Fail# not in [@$!%*?&]
SpacesPass word1!FailSpace not in character class
Empty stringFailBelow minimum length

Test each row in the browser tester before adding it to your test plan. This confirms your expected results are correct — not just your assumptions.

Try It Free — No Signup Required

Runs 100% in your browser. No data is collected, stored, or sent anywhere.

Open Free Regex Tester

Frequently Asked Questions

How do I know if a field accepts a certain character without reading the code?

Paste the validation regex into the browser tester and test with that specific character. If the field's regex highlights a match for your test value, the character is accepted. If not, it will be rejected by the validator.

Can I use the regex tester to understand what test cases to write?

Yes. Paste the developer's regex and systematically test boundary conditions: minimum length, maximum length, allowed special characters, first/last character in each range, and invalid inputs. The match/no-match results directly map to pass/fail expectations in your test cases.

How do I write regex assertions in Playwright?

Playwright accepts regex directly in expect assertions. In Python: expect(locator).to_have_text(re.compile(pattern)). In JavaScript: await expect(locator).toHaveText(/pattern/). Test the pattern in the browser tester first to confirm it matches the expected text format.

Launch Your Own Clothing Brand — No Inventory, No Risk