40pro
40pro

Reputation: 1281

Regular Expression matching characters after a string

For the following expression: ^swlang/(\d{2})/$

matches a string swlang followed by two digit of integers.

Now to modify this expression so that it matches 2 characters instead.

I tried: ^swlang/(\S{2})/$ but it doesn't seem to work.

Note: I am trying to match a URL in django

the full code looks like this

url(r'^swlang/(\d{2})/$', 'klip.views.swlang'),

So for example, a url that is going to be routed to klip.views.swlang would be example.com/swlang/43

The desired one may look like example.com/swlang/en

Upvotes: 1

Views: 7455

Answers (1)

ilomambo
ilomambo

Reputation: 8350

Characters are represented by a group like [a-zA-Z0-9_]

\bswlang/([a-zA-Z0-9_]{2})/?

or you can use the predefined \w

\bswlang/(\w{2})/?$

the first option lets you define whatever character you exactly want the second only accepts letters, numbers and underscore.

After your EDIT I added the slashes, note the last one is /?. It means it may or may not be present in your url

If you use \S as pointed out by others , example.swlang//// would match, if that's what you want fine, otherwise consider use one of my two suggestions.

Note: I changed the ^(beginning of line) for \b (beginning of word) since your example included characters before 'slang'

Upvotes: 2

Related Questions