matlab 正则表达式
regexprep
Replace text using regular expression
collapse all in page
Syntax
Description
replaces the text in newStr = regexprep(str,expression,replace)str that matches expression with the text described by replace. The regexprep function returns the updated text in newStr.
If
stris a single piece of text (either a character vector or a string scalar), thennewStris also a single piece of text of the same type.newStris a single piece of text even whenexpressionorreplaceis a cell array of character vectors or a string array. Whenexpressionis a cell array or a string array,regexprepapplies the first expression tostr, and then applies each subsequent expression to the preceding result.If
stris a cell array or a string array, thennewStris a cell array or string array with the same dimensions asstr. For each element ofstr, theregexprepfunction applies each expression in sequence.If there are no matches to
expression, thennewStris equivalent tostr.
Examples
Update Text
Replace words that begin with M, end with y, and have at least one character between them.
str = 'My flowers may bloom in May';
expression = 'M(\w+)y';
replace = 'April'; newStr = regexprep(str,expression,replace)
newStr = My flowers may bloom in April
Include Tokens in Replacement Text
Replace variations of the phrase 'walk up' by capturing the letters that follow 'walk' in a token.
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.
Include Dynamic Expression in Replacement Text
Replace lowercase letters at the beginning of sentences with their uppercase equivalents using the upperfunction.
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 regular expression matches single characters (.) that follow the beginning of the character vector (^) or a period (\.) and any whitespace (\s*). The replace expression calls the upper function for the currently matching character ($0).
Update Multiple Pieces of Text
Replace each occurrence of a double letter in a set of character vectors with the symbols '--'.
str = { ...
'Whose woods these are I think I know.' ; ...
'His house is in the village though;' ; ...
'He will not see me stopping here' ; ...
'To watch his woods fill up with snow.'};
expression = '(.)\1';
replace = '--';
newStr = regexprep(str,expression,replace)
newStr =
4×1 cell array
'Whose w--ds these are I think I know.'
'His house is in the vi--age though;'
'He wi-- not s-- me sto--ing here'
'To watch his w--ds fi-- up with snow.'
Preserve Case in Original Text
Ignore letter case in the regular expression when finding matches, but mimic the letter case of the original text when updating.
str = 'My flowers may bloom in May';
expression = 'M(\w+)y';
replace = 'April'; newStr = regexprep(str,expression,replace,'preservecase')
newStr = My flowers april bloom in April
Replace Zero-Length Matches
Insert text at the beginning of a character vector using the '^' operator, which returns a zero-length match, and the 'emptymatch' keyword.
str = 'abc';
expression = '^';
replace = '__'; newStr = regexprep(str,expression,replace,'emptymatch')
newStr = __abc
Input Arguments
str — Text to update
character vector | cell array of character vectors | string array
Text to update, specified as a character vector, a cell array of character vectors, or a string array.
Data Types: char | cell | string
expression — Regular expression
character vector | cell array of character vectors | string array
Regular expression, specified as a character vector, a cell array of character vectors, or a string array. Each expression can contain characters, metacharacters, operators, tokens, and flags that specify patterns to match in str.
The following tables describe the elements of regular expressions.
Metacharacters
Metacharacters represent letters, letter ranges, digits, and space characters. Use them to construct a generalized pattern of characters.
|
Metacharacter |
Description |
Example |
|---|---|---|
|
|
Any single character, including white space |
|
|
|
Any character contained within the brackets. The following characters are treated literally: |
|
|
|
Any character not contained within the brackets. The following characters are treated literally: |
|
|
|
Any character in the range of |
|
|
|
Any alphabetic, numeric, or underscore character. For English character sets, |
|
|
|
Any character that is not alphabetic, numeric, or underscore. For English character sets, |
|
|
|
Any white-space character; equivalent to |
|
|
|
Any non-white-space character; equivalent to |
|
|
|
Any numeric digit; equivalent to |
|
|
|
Any nondigit character; equivalent to |
|
|
|
Character of octal value |
|
|
|
Character of hexadecimal value |
|
Character Representation
|
Operator |
Description |
|---|---|
|
|
Alarm (beep) |
|
|
Backspace |
|
|
Form feed |
|
|
New line |
|
|
Carriage return |
|
|
Horizontal tab |
|
|
Vertical tab |
|
|
Any character with special meaning in regular expressions that you want to match literally (for example, use |
Quantifiers
Quantifiers specify the number of times a pattern must occur in the matching text.
|
Quantifier |
Matches the expression when it occurs... |
Example |
|---|---|---|
|
|
0 or more times consecutively. |
|
|
|
0 times or 1 time. |
|
|
|
1 or more times consecutively. |
|
|
|
At least
|
|
|
|
At least
|
|
|
|
Exactly Equivalent to |
|
Quantifiers can appear in three modes, described in the following table. q represents any of the quantifiers in the previous table.
|
Mode |
Description |
Example |
|---|---|---|
|
|
Greedy expression: match as many characters as possible. |
Given the text
|
|
|
Lazy expression: match as few characters as necessary. |
Given the text
|
|
|
Possessive expression: match as much as possible, but do not rescan any portions of the text. |
Given the text |
Grouping Operators
Grouping operators allow you to capture tokens, apply one operator to multiple elements, or disable backtracking in a specific group.
|
Grouping Operator |
Description |
Example |
|---|---|---|
|
|
Group elements of the expression and capture tokens. |
|
|
|
Group, but do not capture tokens. |
Without grouping, |
|
|
Group atomically. Do not backtrack within the group to complete the match, and do not capture tokens. |
|
|
|
Match expression If there is a match with You can include |
|
Anchors
Anchors in the expression match the beginning or end of the input text or word.
|
Anchor |
Matches the... |
Example |
|---|---|---|
|
|
Beginning of the input text. |
|
|
|
End of the input text. |
|
|
|
Beginning of a word. |
|
|
|
End of a word. |
|
Lookaround Assertions
Lookaround assertions look for patterns that immediately precede or follow the intended match, but are not part of the match.
The pointer remains at the current location, and characters that correspond to the test expression are not captured or discarded. Therefore, lookahead assertions can match overlapping character groups.
|
Lookaround Assertion |
Description |
Example |
|---|---|---|
|
|
Look ahead for characters that match |
|
|
|
Look ahead for characters that do not match |
|
|
|
Look behind for characters that match |
|
|
|
Look behind for characters that do not match |
|
If you specify a lookahead assertion before an expression, the operation is equivalent to a logical AND.
|
Operation |
Description |
Example |
|---|---|---|
|
|
Match both |
|
|
|
Match |
|
Logical and Conditional Operators
Logical and conditional operators allow you to test the state of a given condition, and then use the outcome to determine which pattern, if any, to match next. These operators support logical OR, and if or if/else conditions.
Conditions can be tokens, lookaround operators, or dynamic expressions of the form (?@cmd). Dynamic expressions must return a logical or numeric value.
|
Conditional Operator |
Description |
Example |
|---|---|---|
|
|
Match expression If there is a match with |
|
|
|
If condition |
|
|
|
If condition |
|
Token Operators
Tokens are portions of the matched text that you define by enclosing part of the regular expression in parentheses. You can refer to a token by its sequence in the text (an ordinal token), or assign names to tokens for easier code maintenance and readable output.
|
Ordinal Token Operator |
Description |
Example |
|---|---|---|
|
|
Capture in a token the characters that match the enclosed expression. |
|
|
|
Match the |
|
|
|
If the |
|
|
Named Token Operator |
Description |
Example |
|---|---|---|
|
|
Capture in a named token the characters that match the enclosed expression. |
|
|
|
Match the token referred to by |
|
|
|
If the named token is found, then match |
|
|
Note: If an expression has nested parentheses, MATLAB® captures tokens that correspond to the outermost set of parentheses. For example, given the search pattern |
Dynamic Regular Expressions
Dynamic expressions allow you to execute a MATLAB command or a regular expression to determine the text to match.
The parentheses that enclose dynamic expressions do not create a capturing group.
|
Operator |
Description |
Example |
|---|---|---|
|
|
Parse When parsed, |
|
|
|
Execute the MATLAB command represented by |
|
|
|
Execute the MATLAB command represented by |
|
Within dynamic expressions, use the following operators to define replacement text.
|
Replacement Operator |
Description |
|---|---|
|
|
Portion of the input text that is currently a match |
|
|
Portion of the input text that precedes the current match |
|
|
Portion of the input text that follows the current match (use |
|
|
|
|
|
Named token |
|
|
Output returned when MATLAB executes the command, |
Comments
|
Characters |
Description |
Example |
|---|---|---|
(?#comment) |
Insert a comment in the regular expression. The comment text is ignored when matching the input. |
|
Search Flags
Search flags modify the behavior for matching expressions. An alternative to using a search flag within an expression is to pass an option input argument.
|
Flag |
Description |
|---|---|
(?-i) |
Match letter case (default for |
(?i) |
Do not match letter case (default for |
(?s) |
Match dot ( |
(?-s) |
Match dot in the pattern with any character that is not a newline character. |
(?-m) |
Match the |
(?m) |
Match the |
(?-x) |
Include space characters and comments when matching (default). |
(?x) |
Ignore space characters and comments when matching. Use |
The expression that the flag modifies can appear either after the parentheses, such as
(?i)\w*
or inside the parentheses and separated from the flag with a colon (:), such as
(?i:\w*)
The latter syntax allows you to change the behavior for part of a larger expression.
Data Types: char | cell | string
replace — Replacement text
character vector | cell array of character vectors | string array
Replacement text, specified as a character vector, a cell array of character vectors, or a string array, as follows:
If
replaceis a single character vector andexpressionis a cell array of character vectors, thenregexprepuses the same replacement text for each expression.If
replaceis a cell array ofNcharacter vectors andexpressionis a single character vector, thenregexprepattemptsNmatches and replacements.If both
replaceandexpressionare cell arrays of character vectors, then they must contain the same number of elements.regexpreppairs eachreplaceelement with its matching element inexpression.
The replacement text can include regular characters, special characters (such as tabs or new lines), or replacement operators, as shown in the following tables.
|
Replacement Operator |
Description |
|---|---|
|
|
Portion of the input text that is currently a match |
|
|
Portion of the input text that precedes the current match |
|
|
Portion of the input text that follows the current match (use |
|
|
|
|
|
Named token |
|
|
Output returned when MATLAB executes the command, |
|
Operator |
Description |
|---|---|
|
|
Alarm (beep) |
|
|
Backspace |
|
|
Form feed |
|
|
New line |
|
|
Carriage return |
|
|
Horizontal tab |
|
|
Vertical tab |
|
|
Any character with special meaning in regular expressions that you want to match literally (for example, use |
Data Types: char | cell | string
option — Search or replacement option
'once' | N | 'warnings' | 'ignorecase' | 'preservecase' | 'emptymatch' | 'dotexceptnewline' | 'lineanchors' | ...
Search or replacement option, specified as a character vector or an integer value, as shown in the following table.
Options come in sets: one option that corresponds to the default behavior, and one or two options that allow you to override the default. Specify only one option from a set. Options can appear in any order.
|
Default |
Override |
Description |
|---|---|---|
|
|
|
Match and replace the expression as many times as possible (default), or only once. |
|
|
Replace only the |
|
|
|
|
Suppress warnings (default), or display them. |
|
|
|
Match letter case (default), or ignore case while matching and replacing. |
|
|
Ignore case while matching, but preserve the case of corresponding characters in the original text while replacing. |
|
|
|
|
Ignore zero length matches (default), or include them. |
|
|
|
Match dot with any character (default), or all except newline ( |
|
|
|
Apply |
|
|
|
Include space characters and comments when matching (default), or ignore them. With |
Data Types: char | string
Output Arguments
newStr — Updated text
character vector | cell array of character vectors | string array
Updated text, returned as a character vector, a cell array of character vectors, or a string array. The data type of newStr is the same as the data type of str.
More About
Tall Array Support
This function fully supports tall arrays. For more information, see Tall Arrays.
matlab 正则表达式的更多相关文章
- MATLAB 正则表达式(一)(转)
http://blog.sina.com.cn/s/blog_53f29119010009uf.html 正则表达式这个词上大学的时候就听同寝室的一个家伙常念叨——那家伙当然很厉害啦,现在已经发洋财去 ...
- MATLAB里的正则表达式 [转]
正则表达式在处理字符串及文本时显得十分方便,在perl, python等脚本语言,以及java, .net等平台上都支援正则表达式.事实上,在MATLAB中也提供了正则表达式的支持.主要包含三个常用的 ...
- matlab的正则表达式讲解[转]
引言.啥是正则表达式?正则表达式是干啥的?我理解就和我们在word或者其他编辑软件里点的查找.替换的作用是差不多的,不过功能要强大的多,当然使用起来也稍微复杂一些.书上的定义差不多是这样的:正则表达式 ...
- Matlab—regexp正则表达式
原文转自:http://blog.csdn.net/yf210yf/article/details/42421523 关于正则表达式的基本知识 正则表达式就是一个表达式(也是一串字符),它定义了某种字 ...
- matlab的正则表达式
第一部分——单个字符的匹配1 句点符号 '.' ——匹配任意一个(只有一个)字符(包括空格).例如:t.n,它匹配tan. ten.tin和ton,还匹配t#n.tpn甚至t nMatlab例子程序: ...
- 【原创】开源Math.NET基础数学类库使用(03)C#解析Matlab的mat格式
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- Matlab的部分文件操作
Author:Maddock Date:2015-01-20 判断文件是否存在 infilename = [str,'\lena.jpg']; sgc_exist = exist(infilename ...
- matlab clear
clear 删除工作空间中的项目,释放系统内存 语法: clear clear name clear name1 name2 name3... clear global name clear -reg ...
- Matlab和simulink数据的保存和读取
文件的存储 MATLAB支持工作区的保存.用户可以将工作区或工作区中的变量以文件的形式保存,以备在需要时再次导入.保存工作区可以通过菜单进行,也可以通过命令窗口进行. 1. 保存整个工作区 选择Fil ...
随机推荐
- AIX 5.3下创建逻辑卷、添加文件系统并挂载
首先创建逻辑卷smit lv ,这里没多大问题就不细述了. 输入要创建的逻辑卷名.所属卷组.分配多少个LP.创建在哪块磁盘上等,另外还可以设置镜像,默认是只有一份镜像的,即不做mirror. 到此LV ...
- mysql判断一个字符串是否包含某子串 【转】
文章出处:mysql判断一个字符串是否包含某子串 使用locate(substr,str)函数,如果包含,返回>0的数,否则返回0 例子:判断site表中的url是否包含'http://'子串, ...
- 批量删除Windows7中隧道适配器的方法
批量删除Windows7中隧道适配器的方法 1.在网卡属性的"网络"中,将"Internet协议版本(TCP/IPv6)"前面的勾去掉. 2.在CMD下分别执行 ...
- 轻松学习Linux之Shell的常用过滤器
下载高清视频: http://down.51cto.com/data/157818 大小3:MB 时长: 7分钟 更多内容见: Linux爱好者的圣诞大餐-轻松学习Linux系列多媒体 ...
- 用Zebra打造Linux下小型路由器
用Zebra打造Linux下小型路由器 现在的Internet网络相当庞大,不可能在不同的网络之间建立直接的连接,所以这时就必须用路由器为不同网络之间的通信提供路径选择.Linux下搭建路由器价格非常 ...
- BZOJ2716: [Violet 3]天使玩偶(KD-Tree)
Description Input Output Sample Input 100 100 81 23 27 16 52 58 44 24 25 95 34 2 96 25 8 14 97 50 97 ...
- Maven学习总结(14)——Maven 多模块项目如何分工?
一.开场白 使用Maven有段时间了,只能感慨真是个好东西,让我从传统模式体会到了严谨.规范.敏捷.方便的特性. 如果你懂Maven或许看过Juven翻译的<Maven权威指南>: 发个牢 ...
- 删除online日志測试及ora-600 [4194]错误的处理
今天做了一个关于破坏online日志的恢复測试,主要三个场景: 測试1:正常关闭数据库后删除非当前日志 測试2:正常关库后.删除在线日志文件 測试3:非正常关闭数据库.并删除当前在线日志文件 我的測试 ...
- 前端面试题(计算机网络/http/https)
(前端面试题大全,持续更新) 输入url的一系列过程 http缓存(缓存生效的情况),拓展下 get与post的异同,POST一般可以发送什么类型的文件 jsonp有什么不好的地方 http请求头(h ...
- VUE笔记 - 品牌后台 - v-for Splice Some Filter findIndex indexOf 直接return函数结果
<body> <div id="app"> <div class="panel panel-primary"> <di ...