If you’re a multi-discipline developer (like us!) it can be difficult bouncing between languages with different regular expression grammars. There are often command line utilities to help and you can always write code to test your patterns against test data.
That said, there are some great regular expression testing tools online that make it really easy to write and test your regular expression grammars. Our favorite online tool is regex101.com, because it supports PCRE grammars, which is what we use in our AMPS product and surrounding utilities.
Let’s say I have a list of regionalized topics I publish to for both ORDERS and EXECUTIONS, but want to place a subscription that only selects the all ORDERS topics (
^ORDERS_.*$) or everything from the TKO region (
Here’s how you’d use the regex101.com tool to write a fancy regular expression:
- Goto regex101.com!
- Add the list of the topics you publish to in the “TEST STRING” box:
- Add the test pattern to the “REGULAR EXPRESSION” test box, notice I’ve also selected the “pcre” regular expression flavor, because PCRE is the grammar used in AMPS topic regular expression matching:
- You’ll notice that nothing in my pattern matches, but that’s because I need to add a couple of special modifiers to get the multiline matching to work in this sandbox. If you add
mgto the modifier box like in the next snapshot, you’ll see that the correct topics are matching. The
mis for multiline matches where ^ and $ operate on the line rather than the entire input string, which is handy for a list of topics like we’re using. Also, the
gsays you want to match globally, not just the first match.
- Notice also the right-hand side of the page that explains the grammar to you in English with descriptions of all modifiers and a list of frequently used tokens – a nifty feature!
Summary: We hope you find this tool as useful as we do. It really makes testing regular expressions easy and the ability to bounce between Javacript, Python, and PCRE is a real time saver.