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学习中遇到的常见问题: (一) 引 ...
随机推荐
- .Net Core DevOps -免费用Azure四步实现自动化发布(CI/CD)
前言 linux 大行其道的今天想必大家都已经拥抱 core 了吧,通常的方案都是 gitlab+jenkins+centos,但是这样的方案不适合我这种懒人,一直在寻求简单的解决方案,在寻求方案的过 ...
- Python中的inf与nan
Python中可以用如下方式表示正负无穷 >>> float('inf') # 正无穷,inf不区分大小写,float('InF')一样可以. inf >>> fl ...
- 用机智云做PWM占空比控制电机,物联网智能家居应用
因为是新申请的博客,所以申请了总想往里面加点东西,所以把我之前在机智云写的帖子复制了过来 (各位抱歉,由于之前上传的文件可能有错误,之前上传的文件PWM不能用,那么我又重新上传了一个文件,这个文件 ...
- CountDownLatch实现多线程并发请求
package com.test; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Dat ...
- 使用Minifly打造基于视觉感知的跟踪无人机
前言:无人机和人工智能现在是非常热门的话题,将两者结合起来是一个比较好的创意,本文介绍一种可行的解决方案来实现基于视觉感知的跟踪无人机.从零开始搭建无人机系统工作量和难度(以及钱)都是非常大的,所以在 ...
- 通过自制yum源离线安装ansible
系统环境 --CentOS release 7 python版本--Python 3.5.4 背景:在企业环境中,安装ansible的服务器往往不能访问互联网,简单的下载ansible源码安装,会 ...
- win10教育版激活错误:在运行 Microsoft Windows 非核心版本的计算机上,运行"slui.exe ...”
折腾了一天,最终轻松解决,先启用Software Protection服务,在激活(密钥或者工具都行). PS:但是这样还是无法解决Software Protection自动停止的问题,这个可以参考网 ...
- mysql 查询结果显示行号
mysql 查询时,不像oracle那样,可以直接用 rownum 显示结果行号. 可以用定义用户变量来实现 set @myrnum = 0; select (@myrnum := @myrnum + ...
- todaytt
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.Drawe ...
- 纯数据结构Java实现(0/11)(开篇)
为嘛要写 本来按照我的风格,其实很不喜欢去写这些细节的东西,因为笔记上直接带过了. 本来按照我的风格,如果要写,那也是直接上来就干,根本不解释这些大纲,参考依据. 本来按照我的风格,不想太显山露水,但 ...