Replace () with - using regexprep
2 views (last 30 days)
Show older comments
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!!!
0 Comments
Accepted Answer
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
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
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!