Regular Expression Quick Guide

Table

Regular ExpressionUsage
^Matches the beginning of a line
$Matches the end of the line
.Matches any character
\sMatches whitespace
\SMatches any non-whitespace character
*Repeats a character zero or more times
*?Repeats a character zero or more times (non-greedy)
+Repeats a character one or more times
+?Repeats a character one or more times (non-greedy)
[aeiou]Matches a single character in the listed set
[^XYZ]Matches a single character not in the listed set
[a-z0-9]The set of characters can include a range
(Indicates where string extraction is to start
)Indicates where string extraction is to end

Module

  1. Must import the library using import re before using it.
  2. use re.search() to see if a string matches a regular expression, similar to using the find() method for strings
    hand = open('mbox-short.txt')
    for line in hand:
    if line.find('From:') >= 0:
    	print(line)
    
    import re
    hand = open('mbox-short.txt')
    for lin in hand:
    	line = line.rstrip()
    	if re.search('From:', line):
    		print(line)
    
  3. use re.findall() to extract portions of a string that match your regular expression, similar to a combination of find() and slicing: var[5:10]

Examples

  1. using re.search() like startswith():
    hand = open('mbox-short.txt')
    for line in hand:
    	line = line.rstrip()
    	if line.startswith('From:'):
    		print(line)
    
    we fine-tune what is matched by adding special characters to the string
    import re
    
    hand = open('mbox-short.txt')
    for line in hand:
    	line = line.restrip()
    	if re.search('^From:', line):
    		print(line)
    
  2. Depending on how “clean” your data is and the purpose of your application, you may want to narrow your match down a bit.
    在这里插入图片描述
    在这里插入图片描述
  3. To search a regular $ use ‘$’

Matching and extracting data

在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述

Practical applications

  1. String parsing examples
    在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

Reference

  1. regular-expressions
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值