%% Translate Special Character
% Translate a special character in a character vector using the regexptranslate function. Then use the result as a regular expression in regexp.
% Create a character vector that contains the characters '\n'.

chr = 'The sequence \n generates a new line.'

%
% Create a regular expression that finds '\n' as a sequence of the two consecutive characters '\' and 'n'. Since the regexp function interprets '\n' as a newline character, use regexptranslate to create a regular expression to escape '\n'.

pattern = regexptranslate('escape','\n')

%
% Find the starting index of '\n' in chr. To prevent regexp from interpreting '\n' as a newline, use pattern as the regular expression.

idx = regexp(chr,pattern)

%
% Call regexp without escaping '\n'. Since regexp interprets '\n' as a newline, it does not find the literal characters in chr. The regexp function returns an empty array when it does not find a match.

idx = regexp(chr,'\n')

% Copyright 2012 The MathWorks, Inc.