Skip to main content
 首页 » 编程设计

python之使用 python 正则表达式从 URL 中分离 Facebook URL 用户 ID

2025年05月04日29linjiqin

好吧,我从 Stack overflow 中找到了这个正则表达式,它可以隔离 URL 和 ID,但是当我尝试在 App Engine (py) 中使用它时,一个错误弹出窗口说“没有什么可重复的,我知道 Python 中有一个错误关于这个正则表达式重复,谁能解决这个问题?tx

  File "C:\Program Files\Python27\lib\re.py", line 244, in _compile 
    raise error, v # invalid expression 
error: nothing to repeat 

示例代码

#   patt = '(?:http:\/\/)?(?:www.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-]*)?' 
#   tst = self.request.get("form_fbURL") 
#   m=re.match(patt,tst) 

反正我不是一个好的 python 程序员 :)

请您参考如下方法:

有一个urlparse模块来解析url。
urlparse 模块在 Python 3.0 中重命名为 urllib.parse。

$ python 
>>> from urlparse import urlparse 
>>> urlparse('http://www.facebook.com/hello/world.php?id=789') 
ParseResult(scheme='http', netloc='www.facebook.com', path='/hello/world.php', params='', query='id=789', fragment='')