matlab学习一:字符串操作

1、字符串替换replace:newStr = replace(str,old,new)

示例:

str = "12 calls made to: (508) 555-1234";
newStr = replace(str,pat,"(###) ###-####")

结果:newStr = 
"12 calls made to: (###) ###-####"

将所有出现的子字符串 old 替换为 new。如果 old 包含多个子字符串,则 new 必须与 old 具有相同的大小,或者必须为单个子字符串。

2、字符串替换strrep:查找并替换子字符串

​
newStr = strrep(str,old,new) 

将 str 中出现的所有 old 都替换为 new

如果任何输入参数是非标量字符串数组或字符向量元胞数组,则其他输入参数的大小必须兼容。

实例:

chr = 'The quick brown fox'
chr = 
'The quick brown fox'
newChr = strrep(chr,'quick','sly')
newChr = 
'The sly brown fox'

3.regexprep:使用正则表达式替换文本

newStr = regexprep(str,expression,replace) 将 str 中与 expression 匹配的文本替换为 replace 描述的文本。regexprep 函数在 newStr 中返回更新的文本。

  • 如果 str 是一段文本(字符向量或字符串标量),则 newStr 也是一段相同类型的文本。即使 expression 或 replace 是字符向量元胞数组或字符串数组,newStr 也是一段文本。当 expression 为元胞数组或字符串数组时,regexprep 将第一个表达式应用于 str,然后将每个后续表达式应用于前面的结果。

  • 如果 str 是元胞数组或字符串数组,则 newStr 是与 str 具有相同维度的元胞数组或字符串数组。对于 str 的每个元素,regexprep 函数按顺序应用各表达式。

  • 如果不存在与 expression 的匹配项,则 newStr 等同于 str

newStr = regexprep(str,expression,replace,option1,...replaceM) 使用指定的选项修改搜索。例如,指定 'ignorecase' 以执行不区分大小写的匹配。

示例:替换以 M 开头和以 y 结尾且其中至少有一个字符的单词。

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'

2通过在标文中捕获紧随 'walk' 的字母来替换短语 'walk up' 的变体。

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.'

3使用 upper 函数将句子开头的小写字母替换为其大写形式。

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.'
该正则表达式匹配紧随字符向量 (^) 开头的单个字符 (.) 或句点 (\.) 和任何空白 (\s*)。replace 表达式调用 upper 函数以获取当前匹配的字符 ($0)。

4将一组字符向量中的每个双重复字母替换为符号 '--'

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 = 4x1 cell
    {'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.'}

5在查找匹配项时忽略正则表达式中的字母大小写,但在更新时模拟原始文本的字母大小写。

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'

6.使用 '^' 运算符在字符向量开头插入文本,这将返回零长度匹配项,以及 'emptymatch' 关键字。

str = 'abc';
expression = '^';
replace = '__';

newStr = regexprep(str,expression,replace,'emptymatch')
newStr = 
'__abc'

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值