Forum Discussion
Regex for dynamic URI
- Jan 17, 2023
It's hard to do without setting boundaries,
\/main\/.*\/ is the easiest regex to write, it will match any character and any string length between the second and third slash characters. This has no delimiters anyways, so it will match https://test.foo.com/main/7288EA75-5B4C-4931-A1F9-D185077CE5E7/ as well as https://test.foo.com/lots/of/folders/deep/main/7288EA75-5B4C-4931-A1F9-D185077CE5E7/
^\/main\/.*\/ will perform faster because you're adding a "start of string"
^\/main\/.*\/$ is better if nothing comes after the dynamic part because you're adding an "end of string" delimiter
You can also go for better matches, if you can define a pattern in the dynamic data (like in Juergen_Mang suggestion) and/or if you know that the string is X characters long -- it's always best to use all of that information when setting the match conditions.
regex101 is a good tool to help you build it, you can paste strings that should and string that shoudn't match in the text box and see what your regex will be matching.
I've tested the ^\/main\/\w+-[\w-]+\/ syntax in example below, see what matches by yourself
Adding start and end of string is a very good idea. I personaly avoid .* because it is a hungry match. It goes always to the end of the string and then matches backward. This is a performance penalty and can cause other problems, but with such a small regex it should not matter.
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com