>>> import re
>>> s = "Cool Video -> www.youtube.com/watch?v=dQw4w9WgXcQ"
>>> #Look for the part of string you want to upper case
>>> m = re.search(r'^.*(?=\s+->)', s)
>>> m
<_sre.SRE_Match object; span=(0, 10), match='Cool Video'>
>>> #m.start() and m.end() will give you start and endo position of matched string.
>>> new_s = s[m.start():m.end()].upper() + s[m.end():]
>>> #remember that strings are immutable, so make new one
>>> new_s
'COOL VIDEO -> www.youtube.com/watch?v=dQw4w9WgXcQ'
>>> #OR
>>> new_s = m.group().upper() + s[m.end():]
>>> new_s
'COOL VIDEO -> www.youtube.com/watch?v=dQw4w9WgXcQ'
编辑:
另一种方法是查找链接前面的字符串,然后对其应用upper方法:
>>> s = "Cool Video www.youtube.com/watch?v=dQw4w9WgXcQ"
>>> m = re.search(r'(.*)(?=www.*)',s)
>>> s = m.group().upper() + s[m.end():]
>>> s
'COOL VIDEO www.youtube.com/watch?v=dQw4w9WgXcQ'
如果我在这里正确地理解了您的目标,那么您应该首先查找字符串的
upper
大小写部分,然后与原始字符串的其余部分重新连接,如下所示:编辑:
另一种方法是查找链接前面的字符串,然后对其应用
upper
方法:相关问题 更多 >
编程相关推荐