Regular Expressions
Regular expressions allow us to describe patterns in strings. In mathlayer® the following functions handle regular expressions:
regexp,
regexprep and
regexptranslate
We can use the function regexptranslate to do it automatically.
| Expression | Meaning |
| ^ | Matches the beginning of a line |
| $ | Matches the end of the line |
| . | Matches any character |
| \s | Matches whitespace |
| \S | Matches any non-whitespace character |
| * | Repeats a character zero or more times |
| *? | Repeats a character zero or more times (non-greedy) |
| + | Repeats a character one or more times |
| +? | Repeats a character one or more times (non-greedy) |
| [aeiou] | Matches a single character in the listed set |
| [^XYZ] | Matches a single character not in the listed set |
| [a-z0-9] | The set of characters can include a range |
| \w | Matches the ASCII characters [A-Za-z0-9_], stands for "word character" |
regexp
regexp function is used in order to match a regular expression. In case of two input arguments only, regexp returns the indexes where the regular expression has been matched:>> x = 'a 12 3 b 10 c 9 ef'
m = regexp(x, {'[0-9]+'}) % match any number in string x
m{:,:}#
3 6 10 15
>> x = 'a 12 3 b 10 c 9 ef'
m = regexp(x, {'[0-9]+'},'match')
m{:,:}#
[12][3][10][9]
>> str = 'Show which words have the letter h' expression = '\w*h\w*';% any word with 'h'
m = regexp(str,expression,'match')# [Show][which][have][the][h]
>> str = 'Split in different words' splitstring = regexp(str,'\s','split')#% whitespace
[Split][in][different][words]
regexprep
regexprep function replaces matched regular expressions with the desired string:>> str= '<div id="examples"><h3>Examples</h3></div>' regexprep(str, '<.*?>','')#% remove any character between < and >
Examples
Special characters
To use any special characters as literal, we need to escape them with a backslash:>> str = '5+3' regexprep(str, '\+','-')# 5-3
We can use the function regexptranslate to do it automatically.
>> str = '\t means tabulation';
regexp(str, '\t')#
>> exp = regexptranslate('escape', '\t')#
\\t
>> regexp(str, exp)#
1