Python regex从多个字符串中\n提取子字符串

2024-06-25 23:41:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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)。我需要做什么修改


Tags: ofthe字符串research格式dailystring1
1条回答
网友
1楼 · 发布于 2024-06-25 23:41:13

您可以匹配指定的模式:

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

相关问题 更多 >