RegEx.Compile
-------------

Compiles a pattern.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [RegEx](component_RegEx.md) | [4.4](newinversion44.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "RegEx.Compile"; Pattern; CompilerOptions ) 

**MBS**( **"RegEx.Compile";** /\* Compiles a pattern. \*/  
**$Pattern**; /\* The pattern to use.e.g. "H(.\*)" \*/   
**$CompilerOptions**) /\* A number for the options to use. e.g. 1 for case insensitive or 512 for ungreedy. Add 2 for multiline.  
Can be given as text or number.e.g. "multiline¶ungreedy" \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| Pattern | The pattern to use. | "H(.\*)" |
| CompilerOptions | A number for the options to use. e.g. 1 for case insensitive or 512 for ungreedy. Add 2 for multiline.   Can be given as text or number. | "multiline¶ungreedy" |

### Result

Returns reference number or error.

### Description

Compiles a pattern.  
Returns the regular expression reference which you can than pass to other functions.  
Remember to use [RegEx.Release](RegExRelease.md) to free memory later.  
You compile a pattern once. If you use [RegEx.Execute](RegExExecute.md) several times on it, it may be worth using [RegEx.Study](RegExStudy.md) for better performance.  
Please use \\r instead of \\n for the pattern as FileMaker uses Mac line endings.  
RegEx reference numbers are starting at 13000 and counting up for each new expression.  
  
| Compile option | Number | Description |
|---|---|---|
| Caseless | 1 | Do caseless matching |
| Multiline | 2 | ^ and $ match newlines within data |
| Dot All | 4 | . matches anything including NL |
| Extended | 8 | Ignore white space and # comments |
| Anchored | 16 | Force pattern anchoring |
| Dollar End Only | 32 | $ not to match newline at end |
| Ungreedy | 512 | Invert greediness of quantifiers |
| No Auto Capture | 4096 | Disable numbered capturing parentheses (named ones available) |
| Auto Callout | 16384 | Compile automatic callouts |
| FirstLine | 262144 | Force matching to be before newline |
| Dup Names | 524288 | Allow duplicate names for subpatterns |
| Newline CR | 1048576 | Set CR as the newline sequence |
| Newline LF | 2097152 | Set LF as the newline sequence |
| Newline CRLF | 3145728 | Set CRLF as the newline sequence |
| Newline Any | 4194304 | Recognize any Unicode newline sequence |
| Newline Any CRLF | 5242880 | Recognize CR, LF, and CRLF as newline sequences |
| BSR Any CRLF | 8388608 | \\R matches only CR, LF, or CRLF |
| BSR Unicode | 16777216 | \\R matches all Unicode line endings |
| JavaScript Compatible | 33554432 | JavaScript compatibility |
| No start optimize | 67108864 | Disable match-time start optimizations |

  
Please pass sum of the numbers to select options.  
### Examples

Compile with double escapement:

 Set Variable \[ $regex ; Value: MBS ("RegEx.Compile"; "\\\\\\""; 512+1) \]  
Find matches in multiline text:

 Let (\[  
pattern = "^GR.";  
text = "GR1¶GR2 ¶GR3 ";  
CompileOptions = 2 /\* multiline \*/ + 1 /\* caseless \*/ + 4194304 /\* newline any \*/;  
ExecuteOptions = 4194304 /\* newline any \*/;  
regex = MBS ("RegEx.Compile"; pattern; CompileOptions );  
result = MBS ( "[RegEx.FindMatches](RegExFindMatches.md)"; regex; text; ExecuteOptions ; 0 );  
r = MBS ("[RegEx.Release](RegExRelease.md)"; regex)  
\]; result )  
Loop and find all matches for pattern:

 Set Variable \[ $pattern ; Value: "(USt\\-?ID:?|USt\\-?IdNR\\.?:?|Umsatzsteuer\\-?ID:?|VAT\\-?ID:?)\[\\s\]?(DE)\[\\s\]?(\[0-9\]{9})" \]   
Set Variable \[ $ExecuteOptions ; Value: 0 \]   
Set Variable \[ $CompileOptions ; Value: "caseless" \]   
Set Variable \[ $regex ; Value: MBS ("RegEx.Compile"; $Pattern ; $CompileOptions ) \]   
Set Variable \[ $offset ; Value: 0 \]   
  
Loop  
 Set Variable \[ $r ; Value: MBS ("[RegEx.Execute](RegExExecute.md)"; $regex ; RegEx::Text ; $ExecuteOptions ; $offset ) \]   
 Exit Loop If \[ $r &lt; 4 \]   
 Set Variable \[ $s1 ; Value: MBS ("[RegEx.Substring](RegExSubstring.md)"; $RegEx ; 1) \]   
 Set Variable \[ $s2 ; Value: MBS ("[RegEx.Substring](RegExSubstring.md)"; $RegEx ; 2) \]   
 Set Variable \[ $s3 ; Value: MBS ("[RegEx.Substring](RegExSubstring.md)"; $RegEx ; 3) \]   
 Show Custom Dialog \[ $s1 &amp; ¶ &amp; $s2 &amp; ¶ &amp; $s3 \]   
 Exit Loop If \[ MBS ("IsError") \]   
 Set Variable \[ $offset ; $offset + Value: GetAsNumber(GetValue(MBS ("[RegEx.Results](RegExResults.md)"; $regex ); 2)) \]   
End Loop  
Set Variable \[ $r ; Value: MBS ("[RegEx.Release](RegExRelease.md)"; $RegEx ) \]  
### See also

- [RegEx.CaptureCount](RegExCaptureCount.md)
- [RegEx.Execute](RegExExecute.md)
- [RegEx.MinimumLength](RegExMinimumLength.md)
- [RegEx.NameCount](RegExNameCount.md)
- [RegEx.NamedSubstring](RegExNamedSubstring.md)
- [RegEx.Options](RegExOptions.md)
- [RegEx.Release](RegExRelease.md)
- [RegEx.Results](RegExResults.md)
- [RegEx.StringNumber](RegExStringNumber.md)
- [RegEx.Substring](RegExSubstring.md)

### Example Databases

- [RegEx/RegEx Find all matches in text](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/RegEx/RegEx%20Find%20all%20matches%20in%20text.shtml#1ScriptAnchor_)
- [RegEx/RegEx](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/RegEx/RegEx.shtml#1ScriptAnchor_)

### Blog Entries

- [MBS Plugin Advent calendar: 19 - RegEx](https://www.mbsplugins.de/archive/2023-12-19/MBS_Plugin_Advent_calendar_19_/monkeybreadsoftware_blog_filemaker)
- [Regular Expressions in FileMaker](https://www.mbsplugins.de/archive/2023-03-19/Regular_Expressions_in_FileMak/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 26th October 2014 , last changed 26th April 2023

  
[RegEx.CaptureCount](RegExCaptureCount.md) - [RegEx.DataDetector](RegExDataDetector.md)

[HTML Version](RegExCompile.shtml)