MATLAB中的Regex
- regexprep——用于对字符串进行查找并替换。
regexp
Definition:
用于对字符串进行查找,大小写敏感。
- startIndex = regexp(str,expression)
返回与正则表达式指定的字符模式匹配的每个str子字符串的起始索引。如果没有匹配,startIndex就是一个空数组。
- [startIndex,endIndex] = regexp(str,expression)
返回起始索引和结束索引。
- out = regexp(str,expression,outkey)
返回由outkey指定的输出。例如,如果outkey是'match',那么regexp将返回与表达式匹配的子字符串,而不是它们的起始索引。
- [out1,...,outN] = regexp(str,expression,outkey1,...,outkeyN)
用于指定多个输出关键字outkey,获得多个输出。
outkey:
- 'start':起始索引;
- 'end':结束索引;
- 'tokenExtents':返回HTML标签的起始和结束索引;
- 'match':匹配到的文本;
- 'tokens':返回匹配的HTML标签;
- 'names':匹配数值并分配给命名;
- 'split':被expression分隔开的str的非匹配子字符串的文本。
examples:
普通索引匹配
str = 'bat cat can car coat court CUT ct CAT-scan';
expression = 'c[aeiou]+t';
startIndex = regexp(str,expression)
startIndex = 1×2
5 17
多个字符串同时匹配
str = {'Madrid, Spain','Romeo and Juliet','MATLAB is great'};
capExpr = '[A-Z]';
capStartIndex = regexp(str,capExpr);
celldisp(capStartIndex)
capStartIndex{1} =
1 9
capStartIndex{2} =
1 11
capStartIndex{3} =
1 2 3 4 5 6
字符串匹配('match')
str = 'EXTRA! The regexp function helps you relax.';
expression = '\w*x\w*';
matchStr = regexp(str,expression,'match');
celldisp(matchStr)
matchStr{1} =
regexp
matchStr{2} =
relax
非匹配文本
str = 'She sells sea shells by the seashore.';
expression = '[Ss]h.';
[match,noMatch] = regexp(str,expression,'match','split')
match = 1×3 cell 数组
{'She'} {'she'} {'sho'}
combinedStr = strjoin(noMatch,match)
combinedStr = 'She sells sea shells by the seashore.'
捕获HTML标记
str = '<title>My Title</title><p>Here is some text.</p>';
expression = '<(\w+).*>.*</\1>';
[tokens,matches] = regexp(str,expression,'tokens','match');
tokens{1}{1} =
title
tokens{2}{1} =
p
matches{1} =
<title>My Title</title>
matches{2} =
<p>Here is some text.</p>
Enclosing \w+ in parentheses captures the name of the HTML tag in a token. (回溯引用)
命名匹配分配('names')
str = '01/11/2000 20-02-2020 03/30/2000 16-04-2020';
expression = ['(?<month>\d+)/(?<day>\d+)/(?<year>\d+)|'...
'(?<day>\d+)-(?<month>\d+)-(?<year>\d+)'];
tokenNames = regexp(str,expression,'names');
for k = 1:length(tokenNames)
disp(tokenNames(k))
end
month: '01'
day: '11'
year: '2000' month: '02'
day: '20'
year: '2020' month: '03'
day: '30'
year: '2000' month: '04'
day: '16'
year: '2020'
(?<name>\d+) finds one or more numeric digits and assigns the result to the token indicated by name.
regexpi
和regexp用法类似,大小写不敏感。
regexprep
- newStr = regexprep(str,expression,replace)
Replaces the text in str
that matches expression
with the text described by replace
. The regexprep
function returns the updated text in newStr
.
examples
回溯引用替换
str = 'I walk up, they walked up, we are walking up.';
expression = 'walk(\w*?) up';
replace = 'ascend$1'; newStr = regexprep(str,expression,replace)
newStr = 'I ascend, they ascended, we are ascending.'
引用函数(这个似乎只有在MATLAB里面试用,在其他场合如Notepad++并不适用)
str = 'here are two sentences. neither is capitalized.';
expression = '(^|\.)\s*.';
replace = '${upper($0)}'; newStr = regexprep(str,expression,replace)
newStr = 'Here are two sentences. Neither is capitalized.'
The replace expression calls the upper function for the currently matching character ($0).
MATLAB中的Regex的更多相关文章
- MATLAB中绘制质点轨迹动图并保存成GIF
工作需要在MATLAB中绘制质点轨迹并保存成GIF以便展示. 绘制质点轨迹动图可用comet和comet3命令,使用例子如下: t = 0:.01:2*pi;x = cos(2*t).*(cos(t) ...
- matlab 中 eps 的分析
eps(a)是|a|与大于|a|的最小的浮点数之间的距离,距离越小表示精度越高.默认a=1: 这里直接在matlab中输入:eps == eps(1)(true). 我们知道浮点数其实是离散的,有限的 ...
- matlab中patch函数的用法
http://blog.sina.com.cn/s/blog_707b64550100z1nz.html matlab中patch函数的用法——emily (2011-11-18 17:20:33) ...
- paper 121 :matlab中imresize函数
转自:http://www.cnblogs.com/rong86/p/3558344.html matlab中函数imresize简介: 函数功能:该函数用于对图像做缩放处理. 调用格式: B = i ...
- MATLAB中FFT的使用方法
MATLAB中FFT的使用方法 说明:以下资源来源于<数字信号处理的MATLAB实现>万永革主编 一.调用方法X=FFT(x):X=FFT(x,N):x=IFFT(X);x=IFFT(X, ...
- MATLAB中fft函数的正确使用方法
问题来源:在阅读莱昂斯的<数字信号处理>第三章离散傅里叶变换时,试图验证实数偶对称信号的傅里叶变换实部为偶对称的且虚部为零.验证失败.验证信号为矩形信号,结果显示虚部是不为零且最大幅值等于 ...
- Matlab中的一些小技巧
(转于它处,仅供参考) 1.. Ctrl+C 中断正在执行的操作 如果程序不小心进入死循环,或者计算时间太长,可以在命令窗口中使用Ctrl+c来中断.MATLAB这时可能正疲于应付,响应会有些滞后. ...
- Matlab中给figure添加图例(legend),标题(title)和颜色(color)
在Matlab绘图过程中,尤其是需要将多个图绘制在相同的坐标轴中时,通常需要将不同的曲线设置成为不同的颜色.此外,为了直观,还需要给这张图标增添标题和图例.这篇文章展示了在Matlab的绘图窗口(fi ...
- MATLAB中取整函数(fix, floor, ceil, round)的使用
MATLAB取整函数 1)fix(x) : 截尾取整. >> fix( [3.12 -3.12]) ans = 3 -3(2)floor(x):不超过x 的最大整数.(高斯取整) & ...
随机推荐
- python编程——Class(未完成)
__new__ __init__ __call__ __del__ if __name__=='__main__' __main__
- “大屏,您好!” SONIQ声光揭新品“U•F•O”神秘面纱
作为全球第一批做互联网智能电视的传媒企业,SONIQ声光于4月22日在中国大饭店举行了盛大的新品发布会.其中的重头戏就是当天发布会上作为先锋部队入驻中国电视市场的"UFO".笔者作 ...
- [LC] 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- 通过samus驱动实现基本数据操作
传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由(database).集合(collection).文档对象(documen ...
- 引入 JPEGCodec;JPEGImageEncoder; 图片处理;MyEclipse编译时报错处理
在Eclipse中处理图片,需要引入两个包: import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JP ...
- X因素 开启它就能让你成为超级明星
开启它就能让你成为超级明星" title="X因素 开启它就能让你成为超级明星"> "只要努力就能成为明星!"记得电影学院的不少老师都这样告诫学 ...
- HF Java Chap 1
介绍了java的工作方式以及几个有趣的小程序 Java的工作模式 大体来说有四个步骤: 源代码 编译器 编译器的输出 Java虚拟机 源代码 这是我们程序员接触到的部分.根据我们面临的问题,编写一个符 ...
- 树的三种DFS策略(前序、中序、后序)遍历
之前刷leetcode的时候,知道求排列组合都需要深度优先搜索(DFS), 那么前序.中序.后序遍历是什么鬼,一直傻傻的分不清楚.直到后来才知道,原来它们只是DFS的三种不同策略. N = Node( ...
- Quartz Tutorial 11 - Miscellaneous Features of Quartz
文章目录 Plug-Ins Quartz提供了一个接口(org.quartz.spi.SchedulerPlugin) 用于插入附加的功能. 与Quartz一同发布的,提供了各种实用功能的插件可以在o ...
- springboot+jwt实现token登陆权限认证
一 前言 此篇文章的内容也是学习不久,终于到周末有时间码一篇文章分享知识追寻者的粉丝们,学完本篇文章,读者将对token类的登陆认证流程有个全面的了解,可以动态搭建自己的登陆认证过程:对小项目而已是个 ...