Replace () with - using regexprep

2 views (last 30 days)
Zoe Zhang
Zoe Zhang on 6 Sep 2011
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!!!

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 6 Sep 2011
Well, just as simple as your code unless you have other requirements:
DataArr = regexprep(DataArr,'(','-'); % replace ( with -
DataArr = regexprep(DataArr,')',''); % remove )
  3 Comments
Fangjun Jiang
Fangjun Jiang on 6 Sep 2011
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 on 6 Sep 2011
Thanks again! :) I will just keep learning, learning~~

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!