Replace () with - using regexprep

2 次查看(过去 30 天)
Hi,
I have a cell array of strings look like this,
'6,844,828' '(355,537)'
'4,208,098' '(150,830)'
'3,942,604' '(185,044)'
'3,349,374' '(189,507)'
And I need to replace the () with negative signs so that I could convert them to numbers. e.g. (3) actually means -3.
So obviously I could do things like:
DataArr = strrep(DataArr,'(','-'); % replace ( with -
DataArr = strrep(DataArr,')',''); % remove )
And it works, but I really would like to know how to do that using regexprep, some thing like:
regexprep(CSmap, '\(/d{*}\)/','d{*}')
I don't know where to use \ ...
Thanks in advance!!!

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-9-6
Well, just as simple as your code unless you have other requirements:
DataArr = regexprep(DataArr,'(','-'); % replace ( with -
DataArr = regexprep(DataArr,')',''); % remove )
  3 个评论
Fangjun Jiang
Fangjun Jiang 2011-9-6
See doc regexp and follow the link for Regular Expressions. It's a quite complex topic. Mastering regular expressions takes time and practice. It makes my head spin many times. For example, if you have a string s='a1s34da3g7' and you want to pick out only digits, you can use:
>> s='a1s34da3g7';
d=regexp(s,'\d+','match')
d =
'1' '34' '3' '7'
'\d' means any numeric digit,i.e. 0-9
'+' means 1 or more repeatition
Zoe Zhang
Zoe Zhang 2011-9-6
Thanks again! :) I will just keep learning, learning~~

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by