N
N
Nastya19202020-01-01 18:55:41
Java
Nastya1920, 2020-01-01 18:55:41

How can one replace attributes in path using (or not) regex?

To be more precise:
There is a large list of uris in the table (as patterns for defining some rules).
For example, "...localhost:8080/students/{studentId}/grades/{maths}"

I catch requests through the filter, pull out uri, I can get all these attributes by their names.
Now I need to take a string (this uri) and replace these attributes (values) in it with their names: that is, 1234 with "{studentId}" to compare with the patterns from the table.

What is the difficulty: I can’t just search for substring, because such a combination of characters can be found not only among the attributes, but also in the uri endpoints themselves - then I will replace the wrong one (such as students / {students}). Or this combination can be inside another (it is impossible to limit the search condition to the presence of slashes, because the attribute can be at the end of uri.

How can you replace attributes with their names in a string, given all these options? Are there such regexes?
I will be very grateful for your help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2020-01-01
Hasanly @azerphoenix

Hello!
I think that the whole picture of the current situation is better known to you than to me. But!

Or this combination can be inside another (it is impossible to limit the search condition to the presence of slashes, because the attribute can be at the end of uri.

Why not write a regular expression that may or may not contain a trailing slash?
Look at these links. Perhaps they will give you an idea
https://stackoverflow.com/questions/23046572/regex...
https://stackoverflow.com/questions/19132133/regex...
https://stackoverflow.com/questions/ 34733751/regex...
Or, for example, check the location of the attribute and if it is at the end, then ignore the presence of a slash at the end, and in all other cases limit it with a slash.
You can give the exact answer, because the exact structure of all possible urls and patterns available is known to you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question