【问题标题】:MATLAB regexprep with parentheses带括号的 MATLAB 正则表达式
【发布时间】:2016-08-26 09:20:50
【问题描述】:

给定一个字符串元胞数组:

CellArray={'(first)';'second';'x(third)';'four)'; '(...)'};

我想要以下结果:

newCellArray={'first';'second';'x(third)';'four)';'...'};

即仅当括号位于单词的开头和结尾时,我才想删除括号...

我想使用类似的东西:

newCellArray = regexprep(CellArray,expression,replace);

但遗憾的是,尽管尝试了很多次,我还是没有成功......

【问题讨论】:

    标签: regex matlab regexp-replace


    【解决方案1】:

    您可以将beginning and end anchorstoken captureback-replace 一起使用:

    >> expr = '^\((.+)\)$';
    >> CellArray = {'(first)';'second';'x(third)';'four)'; '(...)'};
    >> newCellArray = regexprep(CellArray,expr,'$1')
    
    newCellArray = 
    
        'first'
        'second'
        'x(third)'
        'four)'
        '...'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多