MATLAB: Replacing certain text from a .txt file.

regular expression

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

Best Answer

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?