NLP(四) 正则表达式
原文链接: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(四) 正则表达式的更多相关文章
- spring cloud: zuul(四): 正则表达式匹配其他微服务(给其他微服务加版本号)
spring cloud: zuul(四): 正则表达式匹配其他微服务(给其他微服务加版本号) 比如我原来有,spring-boot-user微服务,后台进行迭代更新,另外其了一个微服务: sprin ...
- 最新自然语言处理(NLP)四步流程:Embed->Encode->Attend->Predict
http://blog.csdn.net/jdbc/article/details/53292414 过去半年以来,自然语言处理领域进化出了一件神器.此神器乃是深度神经网络的一种新模式,该模式分为:e ...
- js备战春招の四の正则表达式详解
正则表达式语法规则:/正则表达式主体/修饰符(可选)什么是正则表达式:正则表达式是用于匹配字符串中字符组合的模式.在 JavaScript中,正则表达式也是对象.这些模式被用于 RegExp 的 ex ...
- Mysql(四)正则表达式
一.正则表达式 1.使用like可以进行不确定的查询(模糊查询),然而,模糊 查询的功能有限,当需要进行更加复杂的模式匹配时,可以 使用正则表达式来完成. 2.正则表达式可以对指定的字符串与模式之间执 ...
- python学习(十四)正则表达式
原文链接 ## 什么是正则表达式`正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑 ...
- PYTHON 爬虫笔记四:正则表达式基础用法
知识点一:正则表达式详解及其基本使用方法 什么是正则表达式 正则表达式对子符串操作的一种逻辑公式,就是事先定义好的一些特定字符.及这些特定字符的组合,组成一个‘规则字符串’,这个‘规则字符串’用来表达 ...
- python 基础(十四) 正则表达式
正则表达式 概念: 正则匹配就是一个模糊的匹配 只要符合我的匹配规则 就会认为是正确的数据(精确的匹配) 1.[] #代表原子表把想要匹配的内容写入原子表中 匹配包含的任意一位字符 [a] ...
- Python学习日记(十四) 正则表达式和re模块
正则表达式: 它是字符串的一种匹配模式,用来处理字符串,可以极大地减轻处理一些复杂字符串的代码量 字符组:它是在同一位置可能出现的各种字符组成了一个字符组,用[]表示,但是它的结果只能是一个数字或者一 ...
- Js笔试题之正则表达式
一.复习字符串的传统操作 如何获取一个字符串中的数字字符,并按数组形式输出,如 dgfhfgh254bhku289fgdhdy675gfh 输出[254,289,675] 分析:循环用charAt() ...
- pl/sql学习(6): 引号/程序调试/列中的字符串合并/正则表达式
有关自治事务的问题: https://www.cnblogs.com/princessd8251/p/4132649.html 我在plsql development学习中遇到的常见问题: (一) 引 ...
随机推荐
- Appium+python自动化(二十五)- 那些让人抓耳挠腮、揪头发和掉头发的事 - 获取控件ID(超详解)
简介 在前边的第二十二篇文章里,已经分享了通过获取控件的坐标点来获取点击事件的所需要的点击位置,那么还有没有其他方法来获取控件点击事件所需要的点击位置呢?答案是:Yes!因为在不同的大小屏幕的手机上获 ...
- 【iOS】The identity used sign the executable is no longer valid.
之前就遇到过这个问题,如图: 今天又遇到了,证书过期的问题. 需要访问苹果开发者的官网 http://developer.apple.com 来解决. 参考:How to fix “The ident ...
- git和githup
一:Git简介 1.1:VCS的历史 Git是一款代码管理工具(Version Control System),傲视群雄,是目前世界上最先进的免费开源的分布式版本控制系统,没有之一! VCS版本控制系 ...
- DesignPattern系列__06迪米特原则
迪米特原则定义 迪米特原则,也叫最少知道原则,即一个类应该对自己依赖的类知道的越少越好,而你被依赖的类多么复杂,对我都没有关系.也就是说,对于别依赖的类来说,不管业务逻辑多么复杂,都应该尽量封装在类的 ...
- 以股票案例入门基于SVM的机器学习
SVM是Support Vector Machine的缩写,中文叫支持向量机,通过它可以对样本数据进行分类.以股票为例,SVM能根据若干特征样本数据,把待预测的目标结果划分成“涨”和”跌”两种,从而实 ...
- C#之BackgroundWorker从简单入门到深入精通的用法总结
需求分析 经常用到的耗时操作,例如: 1.文件下载和上载(包括点对点应用程序传输文件,从网络下载文件.图像等)2.数据库事务(从数据库读到大量的数据到WinForm界面中的DataGridview里呈 ...
- abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十四)
abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...
- jenkins无法连接到git原因
1.账号密码错误 2.公钥私钥不对应(git上为公钥,jenkins为私钥,私钥比公钥长) 3.公钥私钥文件没有复制到jenkins目录下的.ssh文件中
- Go语言-基本的http请求操作
Go发起GET请求 基本的GET请求 //基本的GET请求 package main import ( "fmt" "io/ioutil" "net/ ...
- 一.安全NA之syslog SNMP SSH NTP
一.常用命令 配置模式下: no logging console #关闭屏幕实时显示日志,不影响到日志buffer里(show logging) logging console #打开屏幕实时日志显示 ...