Replacing certain text from a .txt file.

3 ビュー (過去 30 日間)
am
am 2017 年 9 月 13 日
編集済み: Cedric 2017 年 9 月 27 日
How can the following expressions be replaced? For example expressions of the form:
FirstText [Text1@/ Text2@/ Text3] LastText
is to be preplaced by
FirstText Text1 LastText; FirstText Text2 LastText; FirstText Text3 LastText
  2 件のコメント
am
am 2017 年 9 月 27 日
What if there were many words before FirstText and after LastText in str defined above. But I still want the same ouput which is FirstText Text1 LastText; FirstText Text2 LastText; FirstText Text3 LastText
Cedric
Cedric 2017 年 9 月 27 日
What should you get with
FirstText [Text1@/ Text2@/ Text3] MiddleText1 [Text4@/ Text5] MiddleText2 [Text6@/ Text7@/ Text8@/ Text9] LastText

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

採用された回答

Cedric
Cedric 2017 年 9 月 13 日
編集済み: Cedric 2017 年 9 月 13 日
If it is all you have to do, here is an example:
str = 'FirstText [Text1@/ Text2@/ Text3] LastText' ;
tokens = regexp( str, '(.*)[([^@]+)@/ ([^@]+)@/ ([^\]]+)\] (.*)', 'tokens', 'once' ) ;
outStr = sprintf( '%s %s %s; %s %s %s; %s %s %s', tokens{[1,2,5,1,3,5,1,4,5]} ) ;
which outputs
outStr = FirstText Text1 LastText; FirstText Text2 LastText; FirstText Text3 LastText
Note that you don't need regular expressions for this:
tokens = strsplit( str, {' [', '@/ ', '] '} )
tokens =
1×5 cell array
'FirstText' 'Text1' 'Text2' 'Text3' 'LastText'
But what is the context? I suspect that you have to apply this to a more complex case. Could you give a real slice of what you have to process?
  9 件のコメント
Jan
Jan 2017 年 9 月 27 日
'(.*)[([^@]+)@/ ([^@]+)@/ ([^\]]+)\] (.*)'
Be careful. Obviously the angry armadillo has entered your keyboard. [I've grown up in a time, when humor was not marked by smileys, but recognized by mind reading] Nevertheless, +1.
Cedric
Cedric 2017 年 9 月 27 日
編集済み: Cedric 2017 年 9 月 27 日
Well, I've gown up at the same time, but I got in trouble in the mean time making jokes with no smiley, so I am slowwwwly adapting ; <- this is the beginning of a smiley.

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

その他の回答 (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