Remove "( )" from string words

13 ビュー (過去 30 日間)
Isay
Isay 2014 年 10 月 18 日
コメント済み: Jan 2014 年 10 月 19 日

Hi I need to Remove "()" from any word in DataBase.

My Database(cell) have string words such as:

'NEW' 'May' '(AFP)' 'US' 'prosecutors' 'on' 'Friday' 'unveiled' 'indictment' 'including' 'charges' 'of' 'murder' 'and' 'loan' 'sharking' 'against' . . . .

So, I need to Remove "(" and ")" from (AFP). because My word should be : AFP

採用された回答

Robert Cumming
Robert Cumming 2014 年 10 月 19 日
regexp is very compact and powerful - however it can be a bit slow.
If speed is an issue a simple for loop and strrep can sometimes be faster:
db = { 'NEW' 'May' '(AFP)' 'US' 'prosecutors' 'on' };
pattern = '\(|)';
tic
newdb = regexprep(db, pattern, '');
toc
tic
loopDB = db;
for ii=1:length(db)
loopDB{ii} = strrep ( db{ii}, '(', '' );
loopDB{ii} = strrep ( loopDB{ii}, ')', '' );
end
toc
isequal ( newdb, loopDB )
Elapsed time is 0.002665 seconds.
Elapsed time is 0.000087 seconds.
ans =
1
  1 件のコメント
Jan
Jan 2014 年 10 月 19 日
strrep operates on cell strings directly:
loopDB = strrep(db, '(', '');
loopDB = strrep(loopDB, ')', '');

サインインしてコメントする。

その他の回答 (1 件)

Alberto
Alberto 2014 年 10 月 19 日
You should use regexprep to replace anything that matches with parenthesis with an empty string; the pattern that matches any parenthesis shoul be:
pattern = '(\(|\))';
Its necessary to scape ( or ) using \( or \). See the examples:
>> texto = '(AFP)';
regexprep(texto, '(\(|\))', '')
ans =
AFP
>> texto = {'(AFP)','(cro'};
regexprep(texto, '(\(|\))', '')
ans =
'AFP' 'cro'
  1 件のコメント
Guillaume
Guillaume 2014 年 10 月 19 日
Note that you don't need to surround the alternation with brackets and you don't need to escape the closing bracket, so this simpler pattern would work just as well:
pattern = '\(|)';
newdb = regexprep(db, pattern, '');

サインインしてコメントする。

カテゴリ

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