Regular expression capturing patterns

Use our re.group variable to capture regex pattern groups.

VCL

Use this solution in your VCL service (click RUN below to test this solution or clone it to make changes):

Compute@Edge

Use this solution in your Compute@Edge service:

  1. Rust
let url = "/status/200?aaa=foo&bbb=bar";
{
// Capture a group with brackets and regex::captures[n]
lazy_static! {
static ref RE: Regex = Regex::new(r"^/status/(\d+)").unwrap();
}
if let Some(caps) = RE.captures(url) {
assert_eq!(&caps[1], "200");
}
}
{
// Named captures with (?P<name>exp)
lazy_static! {
static ref RE: Regex = Regex::new(r"^/status/(?P<code>\d+)").unwrap();
}
if let Some(caps) = RE.captures(url) {
assert_eq!(&caps["code"], "200");
}
}
{
// Non-capturing groups can be created by prefixing the
// bracketed expression with ?:
lazy_static! {
static ref RE: Regex = Regex::new(r"^/(?:status|code)/(\d+)\?").unwrap();
}
if let Some(caps) = RE.captures(url) {
assert_eq!(&caps[1], "200");
}
}
{
// Bracketed character class with [...]
lazy_static! {
static ref RE: Regex = Regex::new(r"[?&]aaa=([^&]*)").unwrap();
}
if let Some(caps) = RE.captures(url) {
assert_eq!(&caps[1], "foo");
}
}
{
// Specific repeat count with {min,max}
// Alternate options using (a|b)
lazy_static! {
static ref RE: Regex = Regex::new(r"[\?&]a{2,6}=(foo|bar)(?:$|&)").unwrap();
}
if let Some(caps) = RE.captures(url) {
assert_eq!(&caps[1], "foo");
}
}
{
// Make the match case insensitive using an (?i) prefix
lazy_static! {
static ref RE: Regex = Regex::new(r"(?i)AaA=Foo").unwrap();
}
if let Some(caps) = RE.captures(url) {
assert_eq!(&caps[0], "aaa=foo");
}
}