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

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

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

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay