DEV Community

Cover image for Can I get a dash of regular expression in my KQL?
anatdagan
anatdagan

Posted on

3 3

Can I get a dash of regular expression in my KQL?

Some developers love regular expressions, some abhor them.
I belong to the first group.
I know that regular expressions can have a bad impact on performance, and that they are not suitable for every situation. However, every once in a while, they absolutely save the day.

In one of these times I wanted to find in a Kusto table strings with characters that are not English letters, hyphen or underscore.

This is the command that I used:

| extend IllegalChar = extract("[^a-zA-Z\-_]", 0, name)
Enter fullscreen mode Exit fullscreen mode

Easy, right?

I was unpleasantly surprised when Kusto rejected the query with an obscure error message, stating that it can't parse my regular expression.

Immediately I turned to regex101.com, the regular expression buff's best friend. But the regular expression seemed to be valid.

I had to do some digging until I found the issue.
Apparently in SQL the hyphen needs to be in the beginning or end of the sub pattern, and there you don't even have to escape it:

| extend IllegalChar = extract("[^-a-zA-Z_]", 0, name)
Enter fullscreen mode Exit fullscreen mode

I hope this could help someone....

Photo by Hans Eiskonen on Unsplash

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs