繁体   English   中英

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

[英]Python regex to extract substring including \n from multiple strings

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

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) 我需要进行哪些修改?

您可以匹配指定的模式:

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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM