加入收藏 | 设为首页 | 会员中心 | 我要投稿 江门站长网 (https://www.0750zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

Python正则表达式教程-常用文本处理技巧

发布时间:2019-11-05 19:53:39 所属栏目:优化 来源:数据大视界
导读:副标题#e# 介绍: 正则表达式用于识别模式(pattern)是否存在于给定的字符(字符串)序列中。它们有助于处理文本数据,这通常是涉及文本挖掘的数据科学项目的先决条件。您一定遇到过一些正则表达式的应用程序:它们在服务器端用于在注册过程中验证电子邮件地址

如果字符串开头的零个或多个字符与模式匹配,则返回相应的匹配对象。否则None,如果字符串与给定的模式不匹配,则返回。

  1. pattern = "C" 
  2. sequence1 = "IceCream" 
  3. # No match since "C" is not at the start of "IceCream" 
  4. re.match(pattern, sequence1) 
  5. sequence2 = "Cake" 
  6. re.match(pattern,sequence2).group() 
  7. 'C' 

search() 与 match()

该match()函数仅在字符串的开头检查匹配项(默认情况下),而该search()函数在字符串的任何位置检查匹配项。

  • findall(pattern, string, flags=0)

查找整个序列中所有可能的匹配项,并将它们作为字符串列表返回。每个返回的字符串代表一个匹配项。

  1. email_address = "Please contact us at: support@datacamp.com, xyz@datacamp.com" 
  2. #'addresses' is a list that stores all the possible match 
  3. addresses = re.findall(r'[w.-]+@[w.-]+', email_address)for address in addresses:  
  4.  print(address) 
  5. support@datacamp.com 
  6. xyz@datacamp.com 
  • sub(pattern, repl, string, count=0, flags=0)

这就是substitute功能。它返回通过用替换替换或替换字符串中最左边的非重叠模式所获得的字符串repl。如果找不到该模式,则该字符串将原样返回。

  1. email_address = "Please contact us at: xyz@datacamp.com" 
  2. new_email_address = re.sub(r'([w.-]+)@([w.-]+)', r'support@datacamp.com', email_address) 
  3. print(new_email_address) 
  4. Please contact us at: support@datacamp.com 
  • compile(pattern, flags=0)

将正则表达式模式编译为正则表达式对象。当您需要在单个程序中多次使用表达式时,使用该compile()函数保存生成的正则表达式对象以供重用会更有效。这是因为compile()缓存了传递给的最新模式的编译版本以及模块级匹配功能。

  1. pattern = re.compile(r"cookie") 
  2. sequence = "Cake and cookie" 
  3. pattern.search(sequence).group() 
  4. 'cookie' 
  5. # This is equivalent to: 
  6. re.search(pattern, sequence).group() 
  7. 'cookie' 

提示:可以通过指定flags值来修改表达式的行为。您可以flag在本教程中看到的各种功能中添加一个额外的参数。一些使用的标志是:IGNORECASE,DOTALL,MULTILINE,VERBOSE,等。

案例研究:使用正则表达式

通过学习一些示例,您已经了解了正则表达式在Python中的工作方式,是时候动手了!在本案例研究中,您将运用自己的知识。

  1. import reimport requests 
  2. the_idiot_url = 'https://www.gutenberg.org/files/2638/2638-0.txt' 
  3.  
  4. def get_book(url): 
  5.  # Sends a http request to get the text from project Gutenberg 
  6.  raw = requests.get(url).text 
  7.  # Discards the metadata from the beginning of the book 
  8.  start = re.search(r"*** START OF THIS PROJECT GUTENBERG EBOOK .****",raw ).end() 
  9.  # Discards the metadata from the end of the book 
  10.  stop = re.search(r"II", raw).start() 
  11.  # Keeps the relevant text 
  12.  text = raw[start:stop] 
  13.  return text 
  14.  
  15. def preprocess(sentence):  
  16.  return re.sub('[^A-Za-z0-9.]+' , ' ', sentence).lower() 
  17.  
  18. book = get_book(the_idiot_url) 
  19. processed_book = preprocess(book) 
  20. print(processed_book) 

在语料库中找到代词" the"的编号。提示:使用len()功能。

  1. len(re.findall(r'the', processed_book)) 
  2. 302 

(编辑:江门站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!