Python使用Regex删除尾行字符

3ks5zfa0  于 11个月前  发布在  Python
关注(0)|答案(2)|浏览(69)

我正在尝试使用正则表达式删除尾行字符。示例如下

"\\r\\nMacBook 2018\\r\\nApple watch S3\\r\\n11   iPad pro\\\\r\\\\napple pencil"

字符串
我试着像下面这样使用正则表达式。

import re
text = "\\r\\nMacBook 2018\\r\\nApple watch S3\\r\\n11   iPad pro\\\\r\\\\napple pencil"
print(text)
text=re.sub(r'[^\w\s]',' ',text)
print(text)
text=re.sub(r'(?:^| )\w(?:$| )', ' ', text).strip()
print(text)
text=re.sub(r'\s+', ' ', text)
print(text)

\r\nMacBook 2018\r\nApple watch S3\r\n11   iPad pro\\r\\napple pencil
 r nMacBook 2018 r nApple watch S3 r n11   iPad pro  r  napple pencil
nMacBook 2018 nApple watch S3 n11   iPad pro   napple pencil
nMacBook 2018 nApple watch S3 n11 iPad pro napple pencil


我仍然有一些像nmacbook,napple这样的词。我如何正确地清洁它,并有如下结果

MacBook 2018 Apple watch S3 11 iPad pro apple pencil

yyhrrdl8

yyhrrdl81#

尝试以下模式\\\\r\\\\n\\r\\n

import re

text = "\\r\\nMacBook 2018\\r\\nApple watch S3\\r\\n11   iPad pro\\\\r\\\\napple pencil"

clean_text = re.sub(r"(\\\\r\\\\n|\\r\\n)", " ", text)

print(clean_text)

字符串
输出量:
MacBook 2018苹果手表S3 11 iPad pro苹果铅笔


的数据

试着分成两步如果有效,接受答案

text = "\\r\\nMacBook 2018\\r\\nApple watch S3\\r\\n11   iPad pro\\\\r\\\\napple pencil"

# Step 1: Remove the escape characters \r and \n
step1_text = text.replace(r'\r', '').replace(r'\n', '')

# Step 2: Remove backslashes and extra spaces, and create a sentence
words = step1_text.split('\\')
step2_text = ' '.join(words).strip().replace('  ', ' ')

print(step2_text)

m2xkgtsf

m2xkgtsf2#

您始终可以合并操作并替换为空格。
一个选择。

import re

text = "\\r\\nMacBook 2018\\r\\nApple watch S3\\r\\n11   iPad pro\\\\r\\\\napple pencil"

text = re.sub(r'(?:[ \t]*\\+[rn])+[ \t]*|\s*([ \t])\s*', ' ', text).strip()

print(text)

字符串
产出

MacBook 2018 Apple watch S3 11 iPad pro apple pencil


或者,只使用空白\s结构。

import re

text = "\\r\\nMacBook 2018\\r\\nApple watch S3\\r\\n11   iPad pro\\\\r\\\\napple pencil"

text = re.sub(r'(?:\s*\\+[rn])+\s*|\s+', ' ', text).strip()

print(text)

相关问题