Python 在字符串之前删除n
我想删除这个字符串中每个大写单词和数字开头不需要的r和n。我试过正则表达式。不确定正则表达式或其他方法是否有帮助 这是我尝试使用的代码:Python 在字符串之前删除n,python,python-3.x,regex,data-extraction,Python,Python 3.x,Regex,Data Extraction,我想删除这个字符串中每个大写单词和数字开头不需要的r和n。我试过正则表达式。不确定正则表达式或其他方法是否有帮助 这是我尝试使用的代码: text = "nFamily n49 new nTom" regex_pattern = re.compile(r'.*n[A-Z][a-z]*|[0-9]*\s') matches = regex_pattern.findall(text) for match in matches: text = text.replace(
text = "nFamily n49 new nTom"
regex_pattern = re.compile(r'.*n[A-Z][a-z]*|[0-9]*\s')
matches = regex_pattern.findall(text)
for match in matches:
text = text.replace(match," ")
print(text)
预期产出:
Family 49 new Tom
你可以用
text=re.sub(r'\bn(?=[A-Z0-9]),'',文本)
看
详情:
-这里是一个单词的开头\b
-an
字母n
-正向前瞻,要求当前位置右侧立即出现大写ASCII字母或数字(?=[A-Z0-9])
重新导入
rx=r“\bn(?=[A-Z0-9])”
text=“nFamily n49新nTom”
打印(关于子(rx,,,文本))
#=>家庭49新汤姆
使用re.sub(r'\b[rn](?=[A-Z\d]),“”,text)