MATLAB: How to replace string in a line that matches a regular expression and write it to a file

MATLAB

How can I find a match in a string, based on a regular expression (pattern) and replace it with suitable text? How can I write this replaced text to a file?

Best Answer

The "regexprep" function can be used to replace a string based on a pattern or a matching expression, also known as a regular expression in MATLAB. A regular expression is a string that can contain characters, metacharacters, operators, tokens, and flags that specify patterns to be matched in the input string.
The "strfind" function provides a similar functionality of detecting a search string, but is limited to returning the starting index of every occurrence of the search string. In addition, "strfind" is limited to searching for a fixed string and cannot detect patterns.
The "regexp" function gives more flexibility in terms of identifying patterns using regular expressions but is also limited to returning the starting index of every occurrence of the search expression.
Refer to the following documentation pages for "regexprep", "regexp" and "strfind" for further details:
To save the new string to a text file, use the "fprintf" function, which writes the contents of the input string in a file. It is necessary to first open the text file using "fopen" and then close it using "fclose".
For more information on "fprintf", "fopen" and "fclose", refer to the following documentation pages:
The example below demonstrates the usage of the "regexprep" and "fprintf" functions to modify a string based on three different patterns and save the new string to a text file.
In the first example, the word "big" is introduced after every occurrence of "The" or "the" followed by a number.
In the second example, every occurrence of "27.000000" is replace with "28.000000" with a matching number of zeros.
In the third example, every occurrence of "Year 2001" with any number of white spaces between "Year" and "2001" is replace by "Year 2004".
This script also creates a file 'newExpression.txt' that contains the new string with the replaced text.
% Sample script to demonstrate replacing a string occurrence based on a
% pattern, and saving the new string with replaced expression in a text
% file
% The input string
str = ['The 6 quick brown foxes jumped over the 9 black lazy dogs.' char(10) ...
'Value is 27.000000 ' char(10)...
'Year 2001'];
% The regular expression (pattern) to be matched. Notice the first set of
% characters - [tT]. This expression will help capture all occurrences of
% 'the', regardless of the case (t/T) followed by a white space and a number
% from 0-9 (indicated by \d)
expression = '([tT])he (\d)';
% The replacement text. Notice the $1 parameter. This captures the string
% token [tT] and initializes it as an argument to be passed to the
% replacement text. The $2 captures the second token (\d) and passes it to
% the replacement string.
% This enables not knowing the replacement string before
% the script is executed, and find the appropriate replacement in that case
% as well.
replace = '$1he $2 big';
% Replace the text using the "regexprep" function.
newStr = regexprep(str, expression, replace);
% Second example. The token (\d*) searches for a sequence of digits
% of any length (0 character and up), and the expression $1 passes it to
% the replacement string.
newStr = regexprep(newStr, '27.(\d*)', '28.$1');
% Third example. The token (\s+) searches for a sequence of white spaces
% of any length (1 character and up).
newStr = regexprep(newStr, 'Year(\s+)2001', 'Year 2004');
% Open a file for saving (in write (w) mode) the string with replaced text
fileID = fopen('newExpression.txt', 'w');
% Print the contents of 'newStr' to the file referenced by 'fileID'
fprintf(fileID,newStr);
% Close the file
fclose(fileID);