Replace with regexprep in text file

1 回表示 (過去 30 日間)
Tiasa Ghosh
Tiasa Ghosh 2018 年 9 月 6 日
コメント済み: Tiasa Ghosh 2018 年 9 月 6 日
Hello everybody! I am using regexprep to replace a set of characters in a text file.
For example: I am trying to replace +, - , /, ) , (| occurences in my text file with |€. so i used the following code
fid = fopen('repSymbols.txt','wt');
inData = fileread('equations');
fprintf(fid,'%s',regexprep(inData,'[=+)-/(]','€'));
fclose(fid);
But when I use this, it also replaces the occurences of dot . in the file. I don't want the dots to be replaced. Any idea on this?

採用された回答

OCDER
OCDER 2018 年 9 月 6 日
The ")-/" term in the pattern search is actually interpretted as all characters from ")" to "/". So it's the following:
char(uint8(')'):uint8('/'))
ans =
')*+,-./'
Therefore the expanded search pattern does include the ".".
To fix this, use the escape character "\" before the character "-" like this:
inData = '=+)-/(...';
regexprep(inData,'[=+)-/(]','€') %Incorrect
ans =
'€€€€€€€€€'
regexprep(inData,'[=+)\-/(]','€') %Correct
^ add this \
ans =
'€€€€€€...'
  1 件のコメント
Tiasa Ghosh
Tiasa Ghosh 2018 年 9 月 6 日
That worked! thanks

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by