Monday, March 18, 2024

Regular Expressions 101

 /^.*ezsend\/link\/([\w-]+)\/([\w-]+)\/([\w-]+)?.*$

Regular expression sounds very intimating....Good site for testing - https://regex101.com/

Let's break down the regular expression ^.*ezsend\/link\/([\w-]+)\/([\w-]+)\/([\w-]+)?.*$:

^: Asserts the start of the string.

.*: Matches any character (.) zero or more times (*). This part is used to match any characters before the "ezsend/link" part of the URL.

ezsend\/link\/: Matches the literal string "ezsend/link/".

([\w-]+): This is the first capturing group, denoted by the parentheses. It matches one or more word characters (\w) or hyphens (-). Word characters include letters, digits, and underscores.

\/: Matches the forward slash / after the first token.

([\w-]+): This is the second capturing group, which matches the second token similar to the first one.

\/: Matches the forward slash / after the second token.

([\w-]+)?: This is the third capturing group, matching the third token similar to the first two. The ? makes this group optional, allowing the pattern to match URLs with or without a third token.

.*: Matches any remaining characters after the third token, if present.

$: Asserts the end of the string.

Here's how this regex works:

It starts by matching any characters before "ezsend/link/", ensuring that the URL contains this path.

Then, it matches the three tokens separated by forward slashes, allowing hyphens in each token.

The third token is optional, indicated by the ?.

Finally, it matches any remaining characters in the URL until the end of the string.

This regular expression is designed to capture the three tokens from URLs containing hyphens in each token, such as "xSwAAAAAAAA", "l-f1bdGQ92Q", and "ftG-CggAAAA".


No comments:

Post a Comment