Python正则表达式从多个字符串中提取包含\ n的子字符串

时间:2017-11-28 23:44:00

标签: python regex

我想从给定的字符串中提取子字符串 -

string0 = 'Daily wage of the worker0.1- \nNone'
string1 = 'Daily wage of the worker0.2- \nInvalid'
string3 = 'Daily wage of the worker0.3- \nValid'

有50个相似格式的字符串。我想从每一行中提取- \n。什么是正确的正则表达式?

我正在尝试re.search(r'^- \\n$', re.DOTALL)。我需要做出哪些修改?

1 个答案:

答案 0 :(得分:0)

您可以匹配指定的模式:

import re
s = ['Daily wage of the worker0.1- \nNone', 'Daily wage of the worker0.2- \nInvalid', 'Daily wage of the worker0.3- \nValid']
new_s = [re.findall('- \n', i)[0] for i in s]

输出:

['- \n', '- \n', '- \n']