suhailvs
suhailvs

Reputation: 21690

how to match a particular pattern using regex?

I am using django and in urls.py there is a regex pattern:

url(r'^note/(\d+)/$', 'publicnote'),
url(r'^note/$', 'publicnote'),

this works fine. But i decided to add a name to url. so i redesigned the regex:

url(r'^note/(\d*)/?$', 'publicnote',name='public_note'), 

but the problem is this will match note//.

so is there any regex so that it will match only note/ and note/<an integer>/

Upvotes: 0

Views: 77

Answers (1)

Barmar
Barmar

Reputation: 781058

This regexp should work:

r'^note/(?:(\d+)/)?$'

The ? makes the previous group optional.

Upvotes: 1

Related Questions