DEV Community

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

Posted on

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

Discussion (0)