cs531-f19 / discussions

Discussions board for CS 431/531 Web Server Design course
2 stars 12 forks source link

Assignment 2 Redirection RegExp Updated #47

Open ibnesayeed opened 4 years ago

ibnesayeed commented 4 years ago

I have updated Assignment 2 with new redirection pattern. Please update your config files accordingly:

# Status: Incoming RegExp          Redirect URI
302: ^(.*)/coolcar.html$           $1/galaxie.html
302: ^/a2-test/(.*)/1\.[234]/(.*)  /a2-test/$1/1.1/$2
301: ^(.*)/mercury/(.*)$           $1/ford/$2
DavidBittner commented 4 years ago

I had no idea about capture groups in terms of replacement, so thus concludes my 3~ hours of attempting to implement this myself. Oh well. 🤣 🤣

ibnesayeed commented 4 years ago

Creating a basic RegEx parser in itself is an interesting exercise, which basically boils down to creating a state machine, then making it dynamic. Adding support of capture groups to it is the next level task. So, I am glad that you eventually realized that this problem is already solved and implemented in the standard libraries of almost every general purpose modern language.

A tip for you, some languages support named capture groups, which becomes very handy if your RegExp is to be changed which might shift the index of capture groups. Another tip would be the fact that in an unnamed capture group scenario, the numbering of the groups is based on the order non-escaped opening braces appear in the pattern (now you know how would nested capture groups be indexed).

DavidBittner commented 4 years ago

Good to know at the last bit! Implementing a RegEx parser does seem a fun excercise. My hangup was really around the wildcard Regex to match all of the end of the URL. Everything else worked luckily, but once it came down to it, I got to replace all of that code with one line from the RegEx library so it made me feel a little stupid.

ibnesayeed commented 4 years ago

Wildcards are tricky, they also have a greedy match option where the pattern will attempt to identify the smallest portion of the target string for the wildcard that satisfies the whole pattern. There are many online RegExp testers and even visualizers that often use railroad diagram to represent the state machine for a given pattern.