Special characters in regexp

4 ビュー (過去 30 日間)
Prasanna Srisailan
Prasanna Srisailan 2013 年 3 月 28 日
I have a large CSV data file which I process by: [num txt] = xlsread('file.csv') Some variables (in string format) stored in 'txt' have spaces and special characters; which I clear using 'regexprep'. But some of the variables have the 'caret - ^' character which cannot be replaced by this way.
Because of which I cannot designate these as variables and assign them the data.
Is there an alternate way of doing this?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 28 日
You can precede ^ with \
Example
str='abcd^ef';
new_str=regexprep(s,'\^','')
  1 件のコメント
Prasanna Srisailan
Prasanna Srisailan 2013 年 3 月 28 日
Thanks. That works...

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 3 月 28 日
Use \^ to represent literal ^ . See also http://www.mathworks.com/help/matlab/ref/regexptranslate.html
But better yet is to code your expression so that it replaces everything other than specific characters.
regexprep( txt, '[^a-z0-9_]', '' ) %if you want to delete them
If you are looking to turn strings into variable names, you can use http://www.mathworks.com/help/matlab/ref/genvarname.html
But if you are planning to use them as variable names, them please read http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

カテゴリ

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