Whether you're building scalable APIs or managing complex traffic flows, Kong Gateway offers powerful routing capabilities β and at the heart of that power lies one timeless tool: regular expressions (regex).
Regex gives you fine-grained control over how requests are matched, routed, or filtered. But like everything else, Kong's support for regex has evolved significantly β especially with the introduction of expressions in newer versions.
In this post, letβs dive deep into:
- How regex worked in earlier versions of Kong
- Whatβs changed in Kong 3.x and beyond
- Practical use cases and syntax examples
- When to use plain regex and when to switch to expressions
π What is Regex in Kong?
Regular expressions are string patterns that allow matching flexible and complex inputs β such as paths, headers, hostnames, and even query parameters.
In Kong, regex is typically used to:
- Match incoming request paths (e.g., /users/123)
- Match hostnames (e.g., *.example.com)
- Filter headers like X-User-Type: admin
You can write regex patterns in route fields such as paths, hosts, or headers, and Kong will evaluate them to decide whether a request should be routed.
π Regex in Older Kong Versions (Pre-3.x)
In earlier Kong versions, regex was supported β but only within a few specific fields:
- Use regex in paths, like /api/v1/users/\d+
- Use regex in headers, with special syntax like ~ for case-sensitive, and ~* for case-insensitive matching
- Combine multiple route conditions β but they were evaluated separately and limited in complexity
π§ͺ Example 1: Regex in Path
"paths": ["/users/\d+"]
π§ͺ Example 2: Regex in Header
"headers": {
"x-role": ["~^admin$"]
}
β οΈ Limitations in the Past
While powerful, this older approach came with limitations:
- β No way to write complex combined conditions (e.g., header + path regex)
- β Couldnβt easily use request method (GET, POST) in regex logic
- β Static matching β no dynamic plugin control or conditional logic
- β Managing multiple routes with slightly different patterns became messy
β‘ Regex in Modern Kong (v3.0 β v3.7+)
With the evolution of Kong Gateway (starting around v3.0), expressions were introduced β bringing more power, flexibility, and logical operations to routing and plugin control.
β Good News: Regex still works!
Regex is still fully supported in:
- paths
- hosts
- headers
But now, you also get regex support inside expressions, unlocking much more power.
π§ What Are Expressions?
Expressions in Kong are Lua-style conditions that can evaluate fields like:
- request.path
- request.headers
- request.method
- request.query
- request.jwt.claims
And yes β you can use regex functions like string.match() and ngx.re.match() inside them.
π Regex vs Expressions in Kong (Tabular View)
Hereβs how the two approaches compare:
π‘ Real-World Use Cases
- Path Matching with Regex
"paths": ["/orders/\d{4,6}"]
Matches /orders/1234 or /orders/654321.
- Header Matching with Regex
"headers": {
"x-region": ["~^apac-.*$"]
}
Matches x-region: apac-east or apac-west.
- Expression with Regex (New Style)
expression = "ngx.re.match(request.path, '^/reports/[0-9]{4}/[a-z]+$')"
- Case-Insensitive Header Match using Expressions
expression = "string.lower(request.headers['x-department']) == 'finance'"
β Tips for Writing Safe Regex in Kong
- Escape special characters properly in JSON/YAML (use \)
- Use ~* for case-insensitive matches
- Test with https://regex101.com/ before deploying
- Prefer expressions for combined, readable logic
- Be cautious β overly complex regex can slow down high-throughput routes
π¦ Summary
Regex is still a cornerstone of Kong Gatewayβs routing power β but the introduction of expressions has taken it to the next level.
- π·οΈ Older versions = regex only in a few places, limited flexibility
- π§ Newer versions (3.0+) = regex plus full-blown logic with expressions
- π Use regex where it makes sense (simple patterns)
- βοΈ Use expressions for more advanced routing or plugin logic
π§© Final Thoughts
Regex hasnβt gone anywhere β but it now has a smarter companion in Kong expressions.
By combining the two, you can create more dynamic, maintainable, and secure Kong configurations.
So⦠are you still using regex the old way?
Try expressions with regex today β and level up your API Gateway game! π₯

Top comments (0)