我想从给定的字符串中提取子字符串 -
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)
。我需要做出哪些修改?
答案 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']