>>> s = '''
... Name Mak How are you Name non Name anonymouse
... Name Mak1 How are you Name non1
... Name Mak2 How are you Name non2
... '''
>>> re.findall(r'(?m)^Name (\w+)', s)
['Mak', 'Mak1', 'Mak2']
s = '''
Name Mak How are you Name non Name anonymouse
Name Mak1 How are you Name non1
Name Mak2 How are you Name non2
'''
print([line.split()[1] for line in s.splitlines() if line.startswith('Name')])
# Name - matches Name literally
# \s+ - matches one or more of white spaces
# ([^\s]+) - match one or more characters other from whitespace \s and store it inside first capturing group
# .+ - match one or more of any characters (except newline)
您可以使用:
正则表达式详细信息:
(?m)
启用多行模式^
:开始Name
:匹配文本后跟空格(\w+)
:匹配1+个单词字符并在组#1中捕获这是一种使用
str.split
的方法。你知道吗例如:
输出:
详细信息
相关问题 更多 >
编程相关推荐