Regular Expressions in FileMaker
While we have our advanced RegEx functions in MBS FileMaker Plugin using RegEx.Compile and RegEx.Execute, we got a set of easier functions:
RegEx.Match
This function can simply tell you whether there is a match, e.g. when you like to check if some text is contained in a piece of text.
You can play with the data viewer and just evaluate it. e.g.
MBS( "RegEx.Match"; "H(.*)o"; "Hello")
Or use it to check if for example a text is a number by using a pattern like this:
MBS( "RegEx.Match"; "^(\d+)$"; "12345")
The ^ means the beginning of the line (if multi line is enabled) or beginning of the input text. The $ is the end of the line (in multi line) or the end of input in the default mode with single line.
Of course you can pass in flags. You can pass compilation and execution flags. A compiler flag may be something like Caseless vs. CaseSensitive, Multiline or SingleLine, Greedy or Ungreedy, or something for end of line like NewlineAny. Here is a sample where we ask for case insensitive comparison:
MBS( "RegEx.Match"; "H(.*)o"; "hello"; "caseless")
RegEx.MatchList
If you learnt about the function above, you can now apply it to entries in a list. The plugin walks over all items and returns the matches in the list. For example here we find all entries with H before o. Please note that this has no ^ or $, so there can be text before or after the match.
MBS( "RegEx.MatchList"; "H(.*)o"; "Hello¶Test¶World¶House¶Test"; "caseless")
RegEx.Extract
After you enjoyed the match function, so let's now extract what we found. The RegEx.Extract function takes a rewrite parameter which can use backslashes with index numbers to reference to the captures in the pattern. Captures are portions of the text you like to capture and you note that with brackets in the pattern. In the pattern \0 reference to the whole match, which \1 references the first capture.
Let's take a sample code like this to grab the number:
MBS("RegEx.Extract";
"Invoice No. 1234567 from May 2020";
"(Nr|Number|No|Nummer|Nombre|Numbre)([. :]*)(\d+)";
"\3";
"caseless¶greedy")
Our pattern looks for some words related to numbers followed by optional dot, space or double colon. Then it looks for digits following this. There are three captures there for first the number text, then the dots between and finally the number. Our rewrite pattern just picks 3rd capture and returns it, so we get the "1234567" text as result.
MBS("RegEx.Extract";
"Test";
"(.)(.)(.)(.)";
"\1\2\3\4 -> \4\3\2\1")
Here we reference the captures twice in the rewrite pattern, so we output the first text and reverse the characters.
It may also be used to reorder data like for CSV:
MBS("RegEx.Extract";
"123;Test;34;56";
"^(.*);(.*);(.*);(.*)$";
"345;\2;\4;\3")
RegEx.Quote
If you like to find a "*" character and not use it as wildcard, you can use "\*" instead:
MBS("RegEx.Quote"; "Test *")
Returns "Test\ \*" as text.
Especially if you take text from the user and you like to use it within the regular expression, please use this special quoting function.
RegEx.Replace
The replace function is like the extract, but it does search and replace, so the original text is passed through with modifications.
You can reference captures with \0 to \9 as before, so you may use that in your replacements.
RegEx.ReplaceAll
The RegEx.Replace got an option to do replace all, but we got a RegEx.ReplaceAll separately, which basically do the same. After a match was found and replaced, the search continues behind the last match as often as needed.
MBS( "RegEx.ReplaceAll"; "yabba dabba doo"; "b+"; "d" )
Returns "yadda dadda doo".
RegEx.DataDetector
Finally we have the data detector from Apple. Only if you use macOS and iOS, you can pass in text and have the function look for dates, addresses, links, emails, phone numbers or transit information.
Internally a data detector is a specialized regular expression object that matches natural language text for predefined data patterns. Since Apple improves them over time, newer macOS versions can better find something.
See older article about Data Detectors for FileMaker