正则表达式 URL 帮助:单词或短语
Regex URL Help: Word or Phrase
我是正则表达式的绝对菜鸟(我有点了解基础知识,需要帮助一个词或一个短语。如果是一个短语,则用连字符分隔每个词 - :
这是我当前的正则表达式,它只匹配一个词:
r'^streams/search/(?P<stream_query>\w+)/$
?P 只允许 URL 带一个参数。
额外说明:我在 Django urls.py
中使用 python re 模块
有什么建议吗?
这里有一些例子:
game
gsl
starcraft-2014
final-fantasy-iv
word1-word2-word-3
更新说明:
我基本上需要一个正则表达式来扩展当前的,所以在同一个正则表达式中,没有其他的:
r'^streams/search/(?P<stream_query>\w+)/$
所以在这个里面包含新的正则表达式,其中 ?P\w+ 是 Django 认为是参数(并传递到函数中)的任何单词。
URL 定义,其中包括正则表达式:
url(r'^streams/search/(?P\w+)/$', 'stream_search', name='stream_search')
然后,Django 将该参数传递给 stream_search 函数,该函数采用该参数:
def stream_search(request, stream_query):
#here I manipulate the stream_query string, ie: removing the hyphens
因此,我再次需要一个 re 来匹配传递到 stream_query 参数(或如果需要,第二个参数)的单词或短语。
所以,我想要 stream_query 拥有的是:
word1
或
word1-word2-word3
试试这个,
import re
str = "http://example.com/something?id=123&action=yes"
regex = "(query\d+)=(\w+)"
re.findall(regex, str)
您还可以使用 Python 的 urlparse 库,
from urlparse import url parse
urlparse = urlparse("http://example.com/something?id=123&action=yes")
只需拨打 url parse
至 return
ParseResult(scheme='http', netloc='example.com', path='/something', params='', query='id=123&action=yes', fragment='')
如果我正确理解你的问题,那么你可能根本不需要使用正则表达式。
根据你的例子:
example.com/streams/search/rocket-league-fsdfs-fsdfs
看来你要处理的词总是在最后一个/
之后找到。所以你可以 rsplit
然后检查 -
。这是一个例子:
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 you want with the string
else:
#do whatever you want with the string
或匹配 word
或 word-word-word
的正则表达式为:[\w-]+
我是正则表达式的绝对菜鸟(我有点了解基础知识,需要帮助一个词或一个短语。如果是一个短语,则用连字符分隔每个词 - :
这是我当前的正则表达式,它只匹配一个词:
r'^streams/search/(?P<stream_query>\w+)/$
?P 只允许 URL 带一个参数。
额外说明:我在 Django urls.py
中使用 python re 模块有什么建议吗?
这里有一些例子:
game
gsl
starcraft-2014
final-fantasy-iv
word1-word2-word-3
更新说明: 我基本上需要一个正则表达式来扩展当前的,所以在同一个正则表达式中,没有其他的:
r'^streams/search/(?P<stream_query>\w+)/$
所以在这个里面包含新的正则表达式,其中 ?P\w+ 是 Django 认为是参数(并传递到函数中)的任何单词。
URL 定义,其中包括正则表达式: url(r'^streams/search/(?P\w+)/$', 'stream_search', name='stream_search')
然后,Django 将该参数传递给 stream_search 函数,该函数采用该参数:
def stream_search(request, stream_query):
#here I manipulate the stream_query string, ie: removing the hyphens
因此,我再次需要一个 re 来匹配传递到 stream_query 参数(或如果需要,第二个参数)的单词或短语。 所以,我想要 stream_query 拥有的是:
word1
或 word1-word2-word3
试试这个,
import re
str = "http://example.com/something?id=123&action=yes"
regex = "(query\d+)=(\w+)"
re.findall(regex, str)
您还可以使用 Python 的 urlparse 库,
from urlparse import url parse
urlparse = urlparse("http://example.com/something?id=123&action=yes")
只需拨打 url parse
至 return
ParseResult(scheme='http', netloc='example.com', path='/something', params='', query='id=123&action=yes', fragment='')
如果我正确理解你的问题,那么你可能根本不需要使用正则表达式。
根据你的例子:
example.com/streams/search/rocket-league-fsdfs-fsdfs
看来你要处理的词总是在最后一个/
之后找到。所以你可以 rsplit
然后检查 -
。这是一个例子:
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 you want with the string
else:
#do whatever you want with the string
或匹配 word
或 word-word-word
的正则表达式为:[\w-]+