如何删除包含数字,特殊字符,网站网址或电子邮件的整个句子?

时间:2019-01-30 10:03:08

标签: python regex

如何删除包含数字,特殊字符,网站网址或电子邮件的整个句子?

样本输入选项A:

['Hi my name is blank.', 'Do it 3 times.', 'Check out this website: https://blah.com', 'I like pie.', 'My email is asdf@jkl@gmail.com.']

样本输入选项B:

['Hi my name is blank. Do it 3 times. Check out this website: https://blah.com', 'I like pie. My email is asdf@jkl@gmail.com.]

示例输出:

['Hi my name is blank.','I like pie']

当前代码:

def remove_emails(self, dataframe):
    self.log.info('Removing emails from text data')
    no_emails = dataframe.str.replace('\S*@\S*\s?', '')
    return no_emails

def remove_website_links(self, dataframe):
    self.log.info('Removing website links from text data')
    no_website_links = dataframe.str.replace('http\S+', '')
    return no_website_links

def remove_special_characters(self, dataframe):
    self.log.info('Removing special characters from text data')
    no_special_characters = dataframe.replace(r'[^A-Za-z0-9 ]+', '', regex=True)
    return no_special_characters

def remove_numbers(self, dataframe):
    self.log.info('Removing numbers from text data')
    no_numbers = dataframe.str.replace('\d+', '')
    return no_numbers

问题是上面的代码可用于将不需要的字符串替换为空字符串,但是如果它与上面给出的任何正则表达式匹配,我不知道如何删除整个列表元素。对于这些句子提取中的每一个,我也不想多次遍历该列表。总体而言,我要从语料库中删除“不好的”句子。

1 个答案:

答案 0 :(得分:4)

您可以使用此正则表达式检查各种情况,并拒绝与之匹配的字符串。

https?:|@\w+|\d

Python代码,

import re

arr = ['Hi my name is blank.', 'Do it 3 times.', 'Check out this website: https://blah.com', 'I like pie', 'My email is asdf@jkl@gmail.com']

for s in arr:
 m = re.search(r'https?:|@\w+|\d',s)
 if (m):
  pass
 else:
  print(s)

仅产生您想要的句子

Hi my name is blank.
I like pie