Alexander Alexander - 1 year ago 114
Python Question

How to set regex for website url pattern

The url pattern is

This website has similar urls. The unique identifier is
for this url.
The url pattern always has
before word which is at end of url.

I used the following regex


it matched but it match many patterns on this website.

For example regex should match url above but it shouldnt match with

Answer Source

Since you are using a re.match you really need to match the string from the beginning. However, the main problem is that your -p- is inside a character class, and is thus treated as separate symbols that can be matched. Same is with the \w+ - it is considered as \w and + separately.

So, use a sequence:


See this regex demo



See the regex demo

Note that most probably you even have no need in the capture groups, and (...) parentheses can be removed from the pattern.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download