Replace () with - using regexprep

1 visualización (últimos 30 días)
Zoe Zhang
Zoe Zhang el 6 de Sept. de 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!!!

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by