Regular expressions can become quite complex. The lack of white space makes them difficult to read. I can't step though a regular expression with a debugger. So how do experts debug complex regular expressions?
-
2I believe that "testing" your regex is much more important than "debugging" it. You can usually figure what's going on with a regex quite easily looking at the result (or using one of the tools suggested in the answers), but to be really sure it does what you mean you should test your regex with all possible border cases. Testing will eventually clarify what really you want to do and make the debugging useless :) – baol Apr 01 '10 at 14:49
-
7This seems interesting: [http://www.debuggex.com/](http://www.debuggex.com/?re=%5E%28%3F%21%5E%28PRN%7CAUX%7CCLOCK%5C%24%7CNUL%7CCON%7CCOM%5Cd%7CLPT%5Cd%7C%5C..%2A%29%28%5C..%2B%29%3F%24%29%5B%5E%5Cx00-%5Cx1f%5C%5C%3F%2A%3A%5C%22%3B%7C%2F%5D%2B%24&str=.dotfile-not-valid) (Since question is closed I cannot add a real answer.) – KajMagnus Mar 28 '13 at 11:29
-
If you have Visual Studio, you can set a breakpoint near your problem area (eg: `RegEx.Replace(...)`, switch to 'Immediate window' and try out a few `'Regex.IsMatch(yourDebugInputString, yourDebugInputRegEx)` commands to quickly zero in the issue. – DeepSpace101 Apr 21 '14 at 23:13
-
2I'm very surprised nobody seems to have mentioned https://regex101.com/ which has an _actual_ debugger and is web-hosted as well. – mechalynx Jan 22 '17 at 22:25
-
1even in 2017, I believe regexbuddy is still the best tool I can find, and the price remains at $40. I often work at different languages with different regex flavour, so I often get confused. With regexbuddy, it just frees me from the syntax – code4j Jun 28 '17 at 14:16
-
Try CodVerter regex tester: https://codverter.com/src/regextester Online regex tester to validate regular expression patterns. Editor is updating while typing and flags can be changed as desired. Work can be saved local or to the cloud. – Jonathan Applebaum Feb 15 '19 at 16:25
21 Answers
You buy RegexBuddy and use its built in debug feature. If you work with regexes more than twice a year, you will make this money back in time saved in no time. RegexBuddy will also help you to create simple and complex regular expressions, and even generate the code for you in a variety of languages.
Also, according to the developer, this tool runs nearly flawlessly on Linux when used with WINE.
-
33
-
26It runs on Linux via WINE, according to the developer: http://www.regexbuddy.com/wine.html. And about the $40 cost...how much is your time worth? – Mick Feb 27 '10 at 19:54
-
4@KeynnyTM It works under wine, and I get paid to write code so this saves money. – rook Feb 27 '10 at 19:55
-
19
-
39
-
2@Tim: I don't know about others, but it definitely prevents *me* from using it. One of my primary development platforms is a PowerPC Linux machine. – ephemient Feb 27 '10 at 22:28
-
21Well, like Mick said, how much is your time worth? "The best tools money can buy" don't always have to cost money, but sometimes they do. Plus, JGSoft consistently develops *great* quality products with exceptional user service. I have even bought software from them I don't really need (like RegexMagic) because I'd like to support them and keep them in business. You don't know what you're missing. Seriously. – Tim Pietzcker Feb 28 '10 at 05:58
-
+1 My only complaint is that it does not (yet) handle `(?R)` recursive expressions. Otherwise, as Tim says, the quality of all JGsoft software (and the support backing it up) is nothing short of _Excellent!_ (spoken with a Monty Burns accent of course!) – ridgerunner Apr 04 '11 at 00:00
-
2Is that screenshot from Windows 8? I didn't realize it was available back in Feb 2010. – Luke Nov 06 '12 at 11:41
-
1It's an image linked to the developer's website. They must've updated it. – Mick Nov 07 '12 at 15:47
-
3I guess I'm a bit late to the party, but I stumbled over this thread and could not help to recommend http://www.regex101.com/. It does not do everything regexbuddy does, but its get damn near close to it. – Firas Dib Jan 15 '13 at 12:14
-
But seriously, you do a mofo regex debugger and aim it only at Windows users? How's that even sane? There's also perl's rxrx debugger. – Vic Goldfeld Feb 16 '13 at 14:41
-
3@VicGoldfeld: I realize I'm bumping an old thread, but you should check out http://regex101.com/ which now offers a debugger. Check out this example: http://i.imgur.com/Ilz80VV.png or http://regex101.com/r/wS5zF2/#debugger. The debugger only runs PCRE right now. – Firas Dib Oct 05 '13 at 22:46
-
1[regex101](http://regex101.com/) is great all in all a superp service as long as you're writing regular expressions for the three provided flavors. **Unfortunately .net flavor is not part of the list.** So whenever you try to use balanced groups you're toast. – Robert Koritnik Jun 29 '15 at 19:33
-
@kennytm I got this up and running on my MacOS Catalina 10.15.x with no issues by first installing Parallels Desktop, https://www.parallels.com/, and then installing RegexBuddy onto it. Whole process when smooth. Not sure what OS you run, but just FYI. Heck I even paid $70 for Parallels, as I might need to run other Windows apps on my Mac OS. Wine does not run on MacOS Catalina 10.15.x as of right now. – Jose Quijada Jun 24 '21 at 17:29
With Perl 5.10, use re 'debug';
. (Or debugcolor
, but I can't format the output properly on Stack Overflow.)
$ perl -Mre=debug -e'"foobar"=~/(.)\1/' Compiling REx "(.)\1" Final program: 1: OPEN1 (3) 3: REG_ANY (4) 4: CLOSE1 (6) 6: REF1 (8) 8: END (0) minlen 1 Matching REx "(.)\1" against "foobar" 0 <> <foobar> | 1:OPEN1(3) 0 <> <foobar> | 3:REG_ANY(4) 1 <f> <oobar> | 4:CLOSE1(6) 1 <f> <oobar> | 6:REF1(8) failed... 1 <f> <oobar> | 1:OPEN1(3) 1 <f> <oobar> | 3:REG_ANY(4) 2 <fo> <obar> | 4:CLOSE1(6) 2 <fo> <obar> | 6:REF1(8) 3 <foo> <bar> | 8:END(0) Match successful! Freeing REx: "(.)\1"
Also, you can add whitespace and comments to regexes to make them more readable. In Perl, this is done with the /x
modifier. With pcre
, there is the PCRE_EXTENDED
flag.
"foobar" =~ /
(.) # any character, followed by a
\1 # repeat of previously matched character
/x;
pcre *pat = pcre_compile("(.) # any character, followed by a\n"
"\\1 # repeat of previously matched character\n",
PCRE_EXTENDED,
...);
pcre_exec(pat, NULL, "foobar", ...);

- 198,619
- 38
- 280
- 391
I'll add another so that I don't forget it : debuggex
It's good because it's very visual:

- 2,697
- 20
- 24

- 1,138
- 2
- 15
- 32
-
debuggex actually doesn't work for me: the fonts are enforced by JavaScript, apparently, and fail to work (I have monospaced fonts, the firefox console uses them perfectly). As a consequence the display is unusable. In addition, the JavaScript testing hangs the browser with a lot of test cases, for every edition (not only once when fired). Finally, some of the proposed regexes are bugged and do not match as intended. – 7heo.tk Jul 11 '16 at 17:27
When I get stuck on a regex I usually turn to this: https://regexr.com/
Its perfect for quickly testing where something is going wrong.
-
2This tool is really useful: it's web hosted so no installing, it's real time so debugging is a dream, and it even has useful tooltips and a list of special characters, in case you cant remember something. It's perfect, thanks alot – Jason Ridge Aug 30 '12 at 07:50
-
Unfortunately, this tool (still) does not allow you to change the regex delimiters (even for the PCRE engine) - they are fixed as `/` (slashes). This is a showstopper for me. – MrWhite Feb 20 '20 at 01:21
I use Kodos - The Python Regular Expression Debugger:
Kodos is a Python GUI utility for creating, testing and debugging regular expressions for the Python programming language. Kodos should aid any developer to efficiently and effortlessly develop regular expressions in Python. Since Python's implementation of regular expressions is based on the PCRE standard, Kodos should benefit developers in other programming languages that also adhere to the PCRE standard (Perl, PHP, etc...).
(...)
Runs on Linux, Unix, Windows, Mac.

- 21,988
- 13
- 81
- 109

- 562,542
- 136
- 1,062
- 1,124
-
2Kodos doesn't provide true debugging features. You can't step the regex nor pause the execution. – candide Dec 18 '11 at 01:05
-
Any tips on getting it working on a Mac? The Sourceforge site doesn't offer any info on installing for Mac, and my Google-fu seems to be failing me. – Adam Parkin Jul 25 '12 at 15:08
-
Although this would seem to be Python2, and hasn't been updated since 2006 (14 years ago at the time of writing)? – MrWhite Mar 23 '20 at 15:40
I think they don't. If your regexp is too complicated, and problematic to the point you need a debugger, you should create a specific parser, or use another method. It will be much more readable and maintainable.

- 75,622
- 18
- 128
- 150

- 11,667
- 45
- 59
-
4
-
2Everyone will disagree with this, but it's not a bad idea. Everyone assumes that the regex engine is most efficient with enormous regexes. This is not necessarily true, and they're definitely not easy to read. Break your regexes up. – Dan Rosenstark Feb 27 '10 at 20:19
-
1@Michael Brooks: No, before, actually. Having seen the screenshot, I'm okay with the fact you CAN debug a regexp. But I stand on my idea : when a regexp becomes too complicated, it's time to change to another way. – Valentin Rocher Feb 27 '10 at 20:19
There is an excellent free tool, the Regex Coach. The latest version is only available for Windows; its author Dr. Edmund Weitz stopped maintaining the Linux version because too few people downloaded it, but there is an older version for Linux on the download page.

- 144,005
- 19
- 170
- 281
I've just seen a presentation of Regexp::Debugger by its creator: Damian Conway. Very impressive stuff: run inplace or using a command line tool (rxrx), interactively or on a "logged" execution file (stored in JSON), step forward and backward at any point, stop on breakpoints or events, colored output (user configurable), heat maps on regexp and string for optimization, etc...
Available on CPAN for free: http://search.cpan.org/~dconway/Regexp-Debugger/lib/Regexp/Debugger.pm

- 91
- 1
- 1
-
-
-
3@Rook, You can view the presentation at https://www.youtube.com/watch?v=zcSFIUiMgAs – Starfish Jun 05 '13 at 22:46
I use this online tool to debug my regex:
But yeah, it can't beat RegexBuddy.
I debug my regexes with my own eyes. That's why I use /x
modifier, write comments for them and split them in parts. Read Jeffrey Friedl's Mastering Regular Expressions to learn how to develop fast and readable regular expressions. Various regex debugging tools just provoke voodoo programming.
As for me I usually use pcretest utility which can dump the byte code of any regex, and usually it is much more easier to read (for me at least). Example:
PCRE version 8.30-PT1 2012-01-01
re> /ab|c[de]/iB
------------------------------------------------------------------
0 7 Bra
3 /i ab
7 38 Alt
10 /i c
12 [DEde]
45 45 Ket
48 End
------------------------------------------------------------------

- 171
- 2
- 1

- 100,159
- 46
- 371
- 480
If you're a Mac user, I just came across this one:
http://atastypixel.com/blog/reginald-regex-explorer/
It's free, and simple to use, and it's been a great help for me to get to grips with RegExs in general.

- 675
- 6
- 21
Have a look at the (non-free) tools on regular-expressions.info. RegexBuddy in particular. Here is Jeff Atwood's post on the subject.

- 69,215
- 34
- 177
- 229
Writing reg exes using a notation like PCREs is like writing assembler: it's fine if you can just see the corresponding finite state automata in your head, but it can get difficult to maintain very quickly.
The reasons for not using a debugger are much the same as for not using a debugger with a programming language: you can fix local mistakes, but they won't help you solve the design problems that led you to make the local mistakes in the first place.
The more reflective way is to use data representations to generate regexps in your programming language, and have appropriate abstractions to build them. Olin Shiver's introduction to his scheme regexp notation gives an excellent overview of the issues faced in designing these data representations.

- 11,661
- 4
- 46
- 85
-
Parser combinators are indeed an awesome way to go: Parsec and PArrows in Haskell, rsec in Ruby, Boost Spirit in C++, PyParsing in Python, Perl6::Rules in Perl, etc. – ephemient Feb 27 '10 at 20:41
I often use pcretest - hardly a "debugger" but it works over a text-only SSH connection and parses exactly the regex dialect I need: my (C++) code links to libpcre, so there's no difficulty with subtle differences in what's magic and what isn't, etc.
In general I agree with the guy above to whom needing a regex debugger is a code smell. For me the hardest about using regexes is usually not the regex itself, but the multiple layers of quoting needed to make them work.

- 1,088
- 6
- 15
I often use Ruby based regexp tester Rubular
and also in Emacs use M-x re-builder
Firefox also has a useful extension

- 29,401
- 18
- 105
- 117
For me, after having eyeballed the regex (as I'm fairly fluent, and nearly always use /x or equivalent), I might debug rather than test if I am unsure if I would hit some degenerate matching (i.e. something that excessively backtracks) to see if I could solve such issues by modifying the greedyness of an operator for example.
To do that, I'd use one of the methods mentioned above: pcretest, RegexBuddy (if my current workplace has licensed it) or similar, and sometimes I time it in Linqpad if I'm working in C# regexes.
(The perl trick is a new one for me, so will probably add that to my regex toolkit too.)

- 180
- 8