Replace () with - using regexprep

1 回表示 (過去 30 日間)
Zoe Zhang
Zoe Zhang 2011 年 9 月 6 日
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 Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by