原文链接:http://www.one2know.cn/nlp4/

  • * + ?

    * :0个或多个

    + :1个或多个

    ? :0个或1个

    re.search()函数,将str和re匹配,匹配正确返回True
import re

# 匹配函数,输入:文本,匹配模式(即re)
def text_match(text,patterns):
if re.search(patterns,text):
return 'Found a match!'
else:
return 'Not matched!' # 测试
print(text_match('ac','ab?'))
print(text_match('abc','ab?'))
print(text_match('abbc','ab?')) print(text_match('ac','ab*'))
print(text_match('abc','ab*'))
print(text_match('abbc','ab*')) print(text_match('ac','ab+'))
print(text_match('abc','ab+'))
print(text_match('abbc','ab+')) print(text_match('abbc','ab{2}')) print(text_match('aabbbbc','ab{3,5}?'))

输出:

Found a match!
Found a match!
Found a match!
Found a match!
Found a match!
Found a match!
Not matched!
Found a match!
Found a match!
Found a match!
Found a match!
  • $ ^ .

    $ :结尾

    ^ :开头

    . :除换行符以外的任何字符

    \w :字母,数字,下划线

    \s :空格符

    \S :非空格符

    \b :空格

    \B :非空格
import re
def text_match(text,patterns):
if re.search(patterns,text):
return 'Found a match!'
else:
return 'Not matched!' # 任意以a开头,以c结尾
print(text_match('abbc','^a.*c$')) # 以文本开始,后面有出现一次或多次的文本
print(text_match('Tuffy eats pie, Loki eats peas!','^\w+')) # 文末一个或多个\w加上0个或多个非空字符,\S在\w后面表示标点符号
print(text_match('Tuffy eats pie, Loki eats peas!','\w+\S*$')) # 含u在中间的单词
print(text_match('Tuffy eats pie, Loki eats peas!','\Bu\B'))

输出:

Found a match!
Found a match!
Found a match!
Found a match!
  • 字符串匹配

    re.search(pattern,text) :判断text里是否有pattern

    re.finditer(pattern,text) :在text里找到pattern
import re

patterns = ['Tuffy','Pie','Loki']
text = 'Tuffy eats pie, Loki eats peas!' # 匹配字符串
for pattern in patterns:
print('Searching for "%s" in "%s" ->' % (pattern,text))
if re.search(pattern,text):
# 如果不想区分大小写,加参数 flags=re.IGHORECASE
print('Found!')
else:
print('Not Found!') # 匹配字符串,并找到他的位置
pattern = 'eats'
for match in re.finditer(pattern,text):
s = match.start()
e = match.end()
print('Found "%s" at %d:%d'%(text[s:e],s,e))

输出:

Searching for "Tuffy" in "Tuffy eats pie, Loki eats peas!" ->
Found!
Searching for "Pie" in "Tuffy eats pie, Loki eats peas!" ->
Not Found!
Searching for "Loki" in "Tuffy eats pie, Loki eats peas!" ->
Found!
Found "eats" at 6:10
Found "eats" at 21:25
  • 日期,一组字符集合(或字符范围)

    \d :数字

    re.compile() :string => RegexObject的对象

    方括号[]内的所有内容都是OR关系
import re
url = 'http://www.awdawd.com/da/wda/2019/7/2/wda.html' # YYYY/MM/DD
date_regex = '/(\d{4})/(\d{1,2})/(\d{1,2})'
print('Data found in the URL :',re.findall(date_regex,url)) # 有特殊字符返回Flase
def is_allowed_specific_char(string):
charRe = re.compile(r'[^a-zA-Z0-9.]')
string = charRe.search(string)
return not bool(string) print(is_allowed_specific_char('adIDHihdHDIh.'))
print(is_allowed_specific_char('*#$%^&!{}'))

输出:

Data found in the URL : [('2019', '7', '2')]
True
False
  • 找到所有长度为5的单词,缩写替换单词
import re

# 用缩写替换
street = '21 Ramkrishna Road'
print(re.sub('Road','Rd',street)) # 找到长度为5的单词
text = 'Tuffy eats pie, Loki eats bread!'
print(re.findall(r'\b\w{5}\b',text))

输出:

21 Ramkrishna Rd
['Tuffy', 'bread']
  • 基于RE的分词器
import re

raw = 'I am big!  It\'s the pictures that got small.'

# 用一个或多个空格分词
print(re.split(r' +',raw)) # 非 字母数字下划线 分词
print(re.split(r'\W+',raw)) # 匹配分词 !
print(re.findall(r'\w+|\S\w*',raw))

输出:

['I', 'am', 'big!', "It's", 'the', 'pictures', 'that', 'got', 'small.']
['I', 'am', 'big', 'It', 's', 'the', 'pictures', 'that', 'got', 'small', '']
['I', 'am', 'big', '!', 'It', "'s", 'the', 'pictures', 'that', 'got', 'small', '.']
  • 基于RE的词干提取器
import re

# 自己的词干提取器
def stem(word):
split = re.findall(r'^(.*?)(ing|ly|ed|ies|ive|es|s|ment)?$',word)
stem = split[0][0]
return stem # 上节中re分词
raw = 'Keep your friends close, but your enemies closer.'
tokens = re.findall(r'\w+|\S\w*',raw)
print(tokens) # 测试
for t in tokens:
print("'",stem(t),"'")

输出:

['Keep', 'your', 'friends', 'close', ',', 'but', 'your', 'enemies', 'closer', '.']
' Keep ' ' your ' ' friend ' ' close ' ' , ' ' but ' ' your ' ' enem ' ' closer ' ' . '

NLP(四) 正则表达式的更多相关文章

  1. spring cloud: zuul(四): 正则表达式匹配其他微服务(给其他微服务加版本号)

    spring cloud: zuul(四): 正则表达式匹配其他微服务(给其他微服务加版本号) 比如我原来有,spring-boot-user微服务,后台进行迭代更新,另外其了一个微服务: sprin ...

  2. 最新自然语言处理(NLP)四步流程:Embed->Encode->Attend->Predict

    http://blog.csdn.net/jdbc/article/details/53292414 过去半年以来,自然语言处理领域进化出了一件神器.此神器乃是深度神经网络的一种新模式,该模式分为:e ...

  3. js备战春招の四の正则表达式详解

    正则表达式语法规则:/正则表达式主体/修饰符(可选)什么是正则表达式:正则表达式是用于匹配字符串中字符组合的模式.在 JavaScript中,正则表达式也是对象.这些模式被用于 RegExp 的 ex ...

  4. Mysql(四)正则表达式

    一.正则表达式 1.使用like可以进行不确定的查询(模糊查询),然而,模糊 查询的功能有限,当需要进行更加复杂的模式匹配时,可以 使用正则表达式来完成. 2.正则表达式可以对指定的字符串与模式之间执 ...

  5. python学习(十四)正则表达式

    原文链接 ## 什么是正则表达式`正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑 ...

  6. PYTHON 爬虫笔记四:正则表达式基础用法

    知识点一:正则表达式详解及其基本使用方法 什么是正则表达式 正则表达式对子符串操作的一种逻辑公式,就是事先定义好的一些特定字符.及这些特定字符的组合,组成一个‘规则字符串’,这个‘规则字符串’用来表达 ...

  7. python 基础(十四) 正则表达式

    正则表达式 概念: 正则匹配就是一个模糊的匹配 只要符合我的匹配规则 就会认为是正确的数据(精确的匹配) 1.[] #代表原子表把想要匹配的内容写入原子表中   匹配包含的任意一位字符 [a]     ...

  8. Python学习日记(十四) 正则表达式和re模块

    正则表达式: 它是字符串的一种匹配模式,用来处理字符串,可以极大地减轻处理一些复杂字符串的代码量 字符组:它是在同一位置可能出现的各种字符组成了一个字符组,用[]表示,但是它的结果只能是一个数字或者一 ...

  9. Js笔试题之正则表达式

    一.复习字符串的传统操作 如何获取一个字符串中的数字字符,并按数组形式输出,如 dgfhfgh254bhku289fgdhdy675gfh 输出[254,289,675] 分析:循环用charAt() ...

  10. pl/sql学习(6): 引号/程序调试/列中的字符串合并/正则表达式

    有关自治事务的问题: https://www.cnblogs.com/princessd8251/p/4132649.html 我在plsql development学习中遇到的常见问题: (一) 引 ...

随机推荐

  1. java实用类总结

    1.什么是枚举类? 访问修饰符 Enum 枚举名称{}其应用上可以看做一个类去定义,如果枚举里有方法,定义的枚举常量要以':'结尾 2.应用枚举的好处? 枚举限制了范围,更加安全,如果要大量定义常量用 ...

  2. java 第三章

       if  选择结构:        语法:if(条件){ //代码块1 } if——else选择结构 语法:if(条件){ //代码块1 }else{ //代码块2 } 多重if选择结构 语法:i ...

  3. Eclipse "Adb failed to restart !"

    今天遇到这个问题,如图所示: 上网找了下,原来是电脑上的各种手机助手抢占了手机链接.http://blog.csdn.net/zhufuing/article/details/19398125 说得很 ...

  4. Dubbo里面线程池的拒绝策略

    Dubbo里面线程池的拒绝策略 public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy { protecte ...

  5. Wpf窗口设置屏幕居中最前显示

    public Window()         {             InitializeComponent();             WindowStartupLocation = Win ...

  6. 201312-2ISBN号码

    问题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后一位 ...

  7. 洛谷 P2044 [NOI2012]随机数生成器

    题意简述 读入X[0], m, a, c, n和g $ X[n+1]=(a*X[n]+c)\mod m $ 求X数列的第n项对g取余的值. 题解思路 矩阵加速 设\[ F=\begin{bmatrix ...

  8. (二十九)c#Winform自定义控件-文本框(二)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  9. 谷歌hack

    0x00 网上搜集整理的一些可能会用到的Googlehack语法 0x01 intitle: 从网页标题中搜索指定的关键字,可专门用来搜索指定版本名称的各类 web 程序,也可用 allintitle ...

  10. 云上RDS架构

    概述 越来越多的企业选择上云,最基础的云服务就是IaaS(Infrastructure as a Service)服务,直观理解就是虚拟主机,用户不用再自建机房,自己购买服务器,而是直接向云厂商购买虚 ...