例如 url :
xxx/xxx-102.html
xxx/xxx-43.html
想通过"-"以及"."去定位中字符串并存入列表.
请问如何实现?
xxx/xxx-102.html
xxx/xxx-43.html
想通过"-"以及"."去定位中字符串并存入列表.
请问如何实现?
|
okletswinFeb 15, 2017
1
re.match(r'.*-([^\.]+)\.', 'xxx-102.html')
|
|
2
>>> target = 'xxx/xxx-102.html'
>>> result = target[target.find('-') + 1: target.find('.')] >>> result '102' |
|
leavicFeb 15, 2017 via iPhone
3
不想用 re 就做两次 split
|
|
zhichengFeb 15, 2017
4
>>> '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
In [4]: import re
In [5]: re.split(r'[.\-]', 'xxx/xxx-102.html') Out[5]: ['xxx/xxx', '102', 'html'] |
|
ming2050Feb 15, 2017 via iPhone
6
题主是老司机,鉴定完毕
|
|
AntidictatorFeb 16, 2017
7
@mringg 你也是老司机鉴定完毕
|
|
ijustdoFeb 16, 2017
9
>>> a = 'xxx/xxx-102.html'
>>> a.rsplit('.', 1)[0].rsplit('-', 1)[-1] |
|
ijustdoFeb 16, 2017
10
大家要考虑 xxx/xxx 里面也可能包含 . 或者 - 的情况 呵呵 永远不要相信任何输入
|