Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在字符串之前删除n_Python_Python 3.x_Regex_Data Extraction - Fatal编程技术网

Python 在字符串之前删除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(

我想删除这个字符串中每个大写单词和数字开头不需要的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(match," ")
print(text)
预期产出:

Family 49 new Tom
你可以用

text=re.sub(r'\bn(?=[A-Z0-9]),'',文本)

详情:

  • \b
    -这里是一个单词的开头
  • n
    -a
    n
    字母
  • (?=[A-Z0-9])
    -正向前瞻,要求当前位置右侧立即出现大写ASCII字母或数字
见:

重新导入
rx=r“\bn(?=[A-Z0-9])”
text=“nFamily n49新nTom”
打印(关于子(rx,,,文本))
#=>家庭49新汤姆
使用
re.sub(r'\b[rn](?=[A-Z\d]),“”,text)