1
okletswin 2017-02-15 17:18:41 +08:00
re.match(r'.*-([^\.]+)\.', 'xxx-102.html')
|
2
fei051466 2017-02-15 17:43:58 +08:00 1
>>> target = 'xxx/xxx-102.html'
>>> result = target[target.find('-') + 1: target.find('.')] >>> result '102' |
3
leavic 2017-02-15 17:44:51 +08:00 via iPhone
不想用 re 就做两次 split
|
4
zhicheng 2017-02-15 20:22:18 +08:00
>>> 'xxx/xxx-102.html'.rpartition('-')[2].partition('.')[0]
'102' >>> 'xxx/xxx-43.html'.rpartition('-')[2].partition('.')[0] '43' >>> ''.rpartition('-')[2].partition('.')[0] '' >>> '-------------'.rpartition('-')[2].partition('.')[0] '' >>> '.........'.rpartition('-')[2].partition('.')[0] '' >>> '-.-.-.-.-.-.'.rpartition('-')[2].partition('.')[0] '' >>> '-123.-456.-789.-123.-456.-789.'.rpartition('-')[2].partition('.')[0] '789' >>> 'laksdjflkajs8923u41--..asdf92u34100---12342.'.rpartition('-')[2].partition('.')[0] '12342' |
5
imn1 2017-02-15 21:48:18 +08:00 1
In [4]: import re
In [5]: re.split(r'[.\-]', 'xxx/xxx-102.html') Out[5]: ['xxx/xxx', '102', 'html'] |
6
mringg 2017-02-15 23:27:58 +08:00 via iPhone
题主是老司机,鉴定完毕
|
7
Antidictator 2017-02-16 10:16:13 +08:00
@mringg 你也是老司机鉴定完毕
|
9
ijustdo 2017-02-16 17:30:52 +08:00
>>> a = 'xxx/xxx-102.html'
>>> a.rsplit('.', 1)[0].rsplit('-', 1)[-1] |
10
ijustdo 2017-02-16 17:33:35 +08:00
大家要考虑 xxx/xxx 里面也可能包含 . 或者 - 的情况 呵呵 永远不要相信任何输入
|