Forum Discussion

Alfonso_Santia2's avatar
Alfonso_Santia2
Icon for Altostratus rankAltostratus
Jan 17, 2023
Solved

Regex for dynamic URI

Can anyone help on the regex for a dynamic uri, like:
https://test.foo.com/main/7288EA75-5B4C-4931-A1F9-D185077CE5E7
where "7288EA75-5B4C-4931-A1F9-D185077CE5E7" is changing (dynamic)

I have checked other posts on regex with dynamic uri but did not find any that answers this question this is why

  • 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

3 Replies

  • # https://test.foo.com/main/\w+-\w+-\w+-\w+-\w+
    

    \w+ is the right expression to match 1+ alphanumeric characters

  • 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.

  • 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