Exprove
Exprove

Reputation: 1421

Python Regex - Match if capture group does not contain a specific character

I am trying to make a python regex that fails if a capture group contains a specific char.

At the moment this is my regex:

^/api/(?P<username>.+)/$

This regex accepts the following strings:

/api/test/      #Should match
/api/test/test/ #Should not match

However the capture group matches test/test as the username. I would like the regex to fail if "/" is encountered.

Upvotes: 3

Views: 1343

Answers (1)

cs95
cs95

Reputation: 402523

You're trying to avoid any forward-slashes between the second and last one. You can use a character pattern to exclude it in matches:

^/api/(?P<username>[^/]+)/$

Where [^/]+ will match one or more characters that do not consist of a forward-slash.

Upvotes: 2

Related Questions