So I am trying regex but can't seem to get it right

Viewed 46

After doing some string magic these are the strings I want to use as regex


^api/school/$
^api/school/[a-zA-Z0-9]*/$
^api/internal/courses/$
^api/school/search/$
^api/school/[a-zA-Z0-9]*/rooms/$
^api/school/[a-zA-Z0-9]*/rooms/[a-zA-Z0-9]*/$

My Question is why aren't these strings matching to these respectively?

api/school/
api/school/123abc/
api/internal/courses/123ikl
api/school/search/school_name
api/school/school_name/rooms/
api/school/123abc/rooms/2/

2 Answers

1st, 2nd and 6th are fine. 3rd, 4th end with $, means end of String, so extra word "123ikl" and "school_name" will result no match. 5th: [a-zA-Z0-9]* not match with school_name since there is no underscore.

The first, second, and sixth are fine and will match. The third doesn't match because $ matches the end of the string, and the third string you propose extends onward beyond courses/. Likewise for the fourth. The fifth will fail because school_name can't match [a-zA-Z0-9]* since there's no underscore in the character class.

Related