With AI, who's still afraid of regEx? (And why you should still be careful)

Regular expressions, or RegEx, have always had a particular reputation among developers. They're powerful, sometimes essential, but often difficult to write and, above all, difficult to read again a few weeks later.
Who hasn't spent twenty minutes trying to figure out why a regular expression isn't working as expected? Or searched Stack Overflow for a solution that vaguely resembles their problem? Since I started programming, I've lost count of the times I've pulled my hair out over regex.
Today, things have changed. You just need to ask an AI to generate a regular expression to extract IPv6 addresses from a log file, and a few seconds later, you have a suggestion. It's been several years since I've had to struggle with a regex.
No need to know every little detail of the syntax or count parentheses anymore. At least, in theory...
Because while AI makes writing regular expressions considerably easier, it certainly doesn't guarantee that the result is correct. A regex can look perfectly reasonable, work with a few examples, and still fail in real-world situations.
So, is it still worth learning how to write RegEx? And more importantly, can we really trust the ones generated by AI?
The golden age of prompting: RegEx becomes accessible
There's no denying that generative AI has changed the way we approach regular expressions. In the past, writing a slightly complex regex required knowing character classes, quantifiers, capturing groups, lookaheads and lookbehinds, and sometimes the specific features of the regex engine being used.
Today, you can simply describe what you want:
Generate a regular expression to extract IPv4 addresses from a log file.
Or:
Write a regex that detects email addresses regardless of case.
The result arrives almost instantly, often accompanied by an explanation and a few examples. This is particularly useful for developers who only use regular expressions occasionally. There's no need to memorize the entire syntax just to solve a one-off problem.
You can also ask AI to modify an existing expression, simplify it, or adapt it to a particular language such as JavaScript, PHP, or Python.
But there's an important difference between generating an expression that appears to meet a request and producing one that actually covers every expected case. And that's where things get complicated.
The hallucination trap: a regex that seems to work isn't necessarily correct
A generative AI doesn't automatically check every possible case covered by a regular expression. It generates an answer based on statistical patterns learned during training.
It can therefore produce an expression that is syntactically valid but doesn't exactly match the requirements.
Let's take a fairly simple example: extracting IPv4 addresses.
An AI might suggest this expression:
\d{1,3}(?:\.\d{1,3}){3}At first glance, nothing looks wrong. It searches for four groups of digits separated by dots.
The problem is that it also accepts values such as:
999.999.999.999But that's not a valid IPv4 address.
It can also extract an address from within a longer string, without checking the delimiters around it.
This isn't necessarily a syntax error. It's a logic error: the expression doesn't check all the required constraints.
Watch out for edge cases
Things become even more interesting with complex formats.
Take IPv6 addresses. Unlike IPv4 addresses, they can use hexadecimal groups of variable length and a compression notation represented by ::.
A simplified expression like this might seem to do the job:
[0-9a-fA-F:]+However, it also accepts strings that don't correspond to any valid IPv6 address, such as:
::::It can also extract part of a longer string when what we actually want is to identify a complete address.
Another difficulty involves line breaks and special characters.
In JavaScript, the behavior of . depends in particular on the s (dotAll) flag. Without this flag, the dot generally doesn't match line terminators. A regular expression generated to process single-line text may therefore produce different results when given multiline content.
Flags matter too. i makes the search case-insensitive, g finds all occurrences rather than just the first one, and m changes how ^ and $ behave with multiline strings.
An AI might forget one of these flags or add one that isn't needed. In short, a regex can work with three examples and fail on the fourth. And sometimes, it's that fourth example that ends up in production.
Trust but Verify: trust, but verify
The right approach isn't to distrust AI systematically or reject the regular expressions it generates. You simply need to add a validation step to your workflow.
Once the regex has been generated, start by testing it with several examples representative of the data it will actually have to process.
That's exactly what a RegEx testing tool is for: it lets you visualize matches, check capturing groups, and quickly see how an expression behaves with different strings.
For example, you can use our tool:
It lets you test a regular expression directly in your browser, without having to write a dedicated program to check how it behaves.
The idea is to prepare several types of input:
- Typical cases that should match the expression.
- Invalid values that must not be detected.
- Empty or incomplete strings.
- Special characters.
- Spaces and line breaks.
- Edge cases you might not think of straight away.
For example, if your goal is to extract IPv4 addresses, don't just test 192.168.1.1. Also try 999.999.999.999, 192.168.1, or an address embedded in a longer string.
You should also check how the expression behaves in the target language. A regular expression may be accepted by one testing tool and behave differently in another environment, because not all RegEx engines support exactly the same syntax.
Finally, keep in mind that a regex isn't always the best tool for validating data. For some complex formats, such as IPv6 addresses, a dedicated library will often be more reliable than an especially long and difficult-to-maintain regular expression.
AI can also help you test your regular expressions
We often think of using AI to generate a regex, but it can also help with the validation process. For example, you can provide it with a regular expression and ask:
- What types of strings does this expression accept?
- What edge cases could cause problems?
- Can you generate a set of positive and negative test cases?
- Does this expression work with the JavaScript RegEx engine?
This is an interesting way to take a step back and review an expression you didn't write yourself.
However, AI can also get its analysis wrong. The examples it generates need to be checked too. For an important feature, it's best to keep these test cases in a proper automated test suite.
Watch out for performance: some regexes can crash your application
Another, less obvious problem involves performance. Some regular expressions can require enormous amounts of resources from the engine executing them. This is particularly true when they trigger a phenomenon known as catastrophic backtracking.
Take this expression:
^(a+)+$It looks harmless: it checks whether a string contains only a characters. However, when given a long sequence of a characters followed by a different character, the engine may try an exponential number of combinations before concluding that there's no match.
With an expression like this, just a few dozen characters can be enough to cause a very long processing time, or even block the thread executing the regex. This problem has a name: ReDoS (Regular Expression Denial of Service). It can even become a security vulnerability if a malicious user can control the input string being analyzed.
So, what about AI? It can easily generate an expression that works with a few examples without detecting this complexity issue. That's why checking whether a regex returns the expected result isn't enough: you also need to test its behavior with long or specially crafted inputs, and watch out for nested quantifiers.
Conclusion
AI has clearly made regular expressions much less intimidating. It's now possible to generate, modify, or understand a regex without mastering its entire syntax.
That's a welcome time-saver, particularly for developers who don't write them every day.
But be careful not to confuse a regular expression that works with a few examples with one that actually meets the requirements. A misplaced parenthesis, a forgotten flag, or an overlooked edge case can be enough to introduce a bug that's difficult to detect.
So yes, you can ask AI to generate your RegEx. But take a few extra seconds to test them. It might save you several hours of debugging later.
What bothers me most, in the end, is that my colleagues no longer ask me for help with their regexes. I feel a little less useful than I used to… I guess AI has also replaced some of those little moments when I could share my experience. We'll see where things stand in a few years' time with generative AI.





