python - Regex URL Help: Word or Phrase -


i absolute noob @ regex (i kind of know basics , need word, or phrase. if phrase, separate each word hyphen - :

this current regex, matches 1 word:

r'^streams/search/(?p<stream_query>\w+)/$ 

the ?p allows url take parameter.

extra note: using python re module django urls.py

any suggestions?

here examples:

game gsl starcraft-2014 final-fantasy-iv word1-word2-word-3 

updated explanation: need regular expression expand current one, inside same regex, no other one:

r'^streams/search/(?p<stream_query>\w+)/$ 

so include new regex inside one, ?p\w+ word django considers parameter (and passed function).

url definition, includes regex: url(r'^streams/search/(?p\w+)/$', 'stream_search', name='stream_search')

then, django passes parameter stream_search function, takes parameter:

def stream_search(request, stream_query):     #here manipulate stream_query string, ie: removing hyphens 

so, once again, need re match word or phrase, passed stream_query parameter (or if necessary, second one). so, want stream_query have is:

word1 

or word1-word2-word3

if understand question correctly might not have use regexs @ all.

based on example:

example.com/streams/search/rocket-league-fsdfs-fsdfs 

it seems term want deal found after last /. can rsplit , check -. here example:

url = "example.com/streams/search/rocket-league-fsdfs-fsdfs" result = url.rsplit("/", 1)[-1] #result = ["example.com/streams/search", "rocket-league-fsdfs-fsdfs"] if "-" in result:     #do whatever want string else:     #do whatever want string 

or regex match either word or word-word-word be: [\w-]+


Comments