首页 > 解决方案 > 在特殊字符 Python RegEx 之后获取字符串

问题描述

如何在特殊字符后获取字符串?

例如我想在/in之后获取字符串

my_string = "Python/RegEx"

输出为:RegEx

我试过了 :

h = []
a = [ 'Python1/RegEx1' , 'Python2/RegEx2', 'Python3/RegEx3']

for i in a: 
    h.append(re.findall(r'/(\w+)', i))
print(h)

但输出是:[['RegEx1'], ['RegEx2'], ['RegEx3']]

我需要 :['RegEx1', 'RegEx2', 'RegEx3']

提前致谢

正则表达式初学者

标签: pythonregex

解决方案


使用.extend()

for i in a: 
    h.extend(re.findall(r'/(\w+)', i))

使用+=(只是另一种调用方式.extend):

for i in a: 
    h += re.findall(r'/(\w+)', i)

使用解包:

for i in a: 
    h.append(*re.findall(r'/(\w+)', i))

推荐阅读