可以从文本中删除文本吗?

时间:2019-08-11 01:03:51

标签: jupyter-notebook python-3.6

Por ejemplo tengo:“ muy muy muy real real que esto no funcionaaaaaaaa”和quisiera tener“ muy real que esto no funciona”

  

text = re.sub(r'(\ w)\ 1+',r'\ 1','muy muy muy real real que esto no funcionaaaaaa'

     

'muy muy muy real real que esto no funciona'

解决问题的问题的解决之道。

Necesito que sea utilizando re.sub

Gracias。

1 个答案:

答案 0 :(得分:0)

在数据科学方面Stackexchange post

import re
sentence = 'I need need to learn regex... regex from scratch!'

# remove punctuation
# the unicode flag makes it work for more letter types (non-ascii)
no_punc = re.sub(r'[^\w\s]', '', sentence, re.UNICODE)
print('No punctuation:', no_punc)

# remove duplicates
re_output = re.sub(r'\b(\w+)( \1\b)+', r'\1', no_punc)
print('No duplicates:', re_output)