网上找
请问python中正则表达式如何获取字符串中间指定内容,例如 "forum/135/topic/794150" 我分别想获得135 和794150,正则表达式应该怎么写? 在网上找到的方法都不太对,恳请大牛指教~
1
2
3
4
|
import re pattern = re. compile ( "forum\/(\d*)\/topic\/(\d*)" ) res = pattern.search( 'forum/135/topic/794150' ).groups() print res[ 0 ],res[ 1 ] |
实际自己写出的
pattern = re.compile("q=(.*?)&")
体会.*?的作用