Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal


RegularExpressions in MQL4 for working with regular expressions - library for MetaTrader 4

| English Русский 中文 Español Deutsch 日本語 Português

Published by:
MetaQuotes Software Corp.
votes: 13
2017.10.04 12:09
2017.10.09 10:13
\MQL4\mql4\Experts\RegExpressions Demo\
Tests.mq4 (139.5 KB)view
Array.mqh (22.36 KB)view
Wrappers.mqh (17.96 KB)view
Collection.mqh (12.54 KB)view
Dictionary.mqh (42.58 KB)view
IComparer.mqh (6.62 KB)view
IEnumerator.mqh (10.39 KB)view
IList.mqh (5.7 KB)view
LinkedList.mqh (28.29 KB)view
List.mqh (38.77 KB)view
TimeSpan.mqh (31.7 KB)view
Regex.mqh (57.05 KB)view
RegexCode.mqh (22.25 KB)view
RegexFCD.mqh (31.25 KB)view
RegexGroup.mqh (8.72 KB)view
RegexMatch.mqh (22.94 KB)view
RegexNode.mqh (32.45 KB)view
RegexParser.mqh (93.16 KB)view
RegexTree.mqh (8.47 KB)view
RegexWriter.mqh (24.26 KB)view

Real author:

Microsoft Corporation. Source code taken from .Net Framework 4.6.1

Note: The library works on the MetaTrader 4 build 1384 and higher.

Unzip the archive into the terminal_data_folder.
The library codes are located in the: <terminal_data_folder>\MQL4\Include\RegularExpressions\
Sample test scripts can be found in the <terminal_data_folder>\MQL4\Scripts\RegularExpressionsExamples\

Here is a translation of the RegularExpressions from .Net Framework 4.6.1.

To work with the library, include the Regex.mqh file from the "\MQL4\Include\RegularExpressions\" directory in your code.

Several illustrative examples are provided with the library, which at the same time serve as the test cases. All the samples are taken from the official site of Microsoft Corporation, they vividly demonstrate the main differences from the regular expressions in C# and the features of their use in the MQL4.

Below is more detailed information about RegularExpressions MQL4 ported library packages:

Archived txt file to determine the Unicode categories for all symbols (including non-Latin characters).
Represents the results from a single successful subexpression capture.
Represents the set of captures made by a single capturing group.
Represents the results from a single capturing group.
RegexGroupCollections.mqhRepresents a collection of Group objects.
RegexMatch.mqhRepresents the results from a single regular expression match.
RegexMatchCollection.mqhRepresents a collection of successful matches found by iteratively applying the regular expression pattern to the input string.
Regex.mqhRepresents an immutable regular expression
RegexOptions.mqh Provides enumerated values to use to set regular expression options.

The regular expression parameters from the file RegexOptions.mqh:

Specifies that no options are set.
Specifies case-insensitive matching.
MultilineSpecifies multiline mode.
ExplicitCaptureDo not capture unnamed groups. Specifies that the only valid captures are explicitly named or numbered groups of the form (?<name> subexpression).
SinglelineSpecifies single-line mode.
IgnorePatternWhitespaceEliminates unescaped white space from the pattern and enables comments marked with #.
RightToLeftSpecifies that the search will be from right to left instead of from left to right.
DebugSpecifies that the program works in the debug mode.
ECMAScriptEnables ECMAScript-compliant behavior for the expression. This value can be used only in conjunction with the IgnoreCase and Multiline values.

Working with RegularExpressions for MQL4:

  1. As withe the version for .Net, this library implements a storage (static cache) of regular expressions. All implicitly created regular expressions (instances of the Regex class) are written to that storage. This approach speeds up the operation of the scripts, as it eliminates the need to rebuild the regular expression if its pattern matches any of the existing ones. The default size of the storage is 15. The Regex::CacheSize() method returns or sets the maximum number of entries in the current static cache of the complied regular expressions.
  2. The second feature of working with regular expression in MQL4 directly follows from the first one. And it lies in the fact that the above storage must be cleared. To do that, call the Regex::ClearCache() static function. It is recommended to call this function only after the work with the regular expressions has been completed, otherwise there is a risk to delete necessary pointers.
  3. Unlike the .Net, the MQL4 does not implement the foreach loop, and hence the enumeration handling will be different. Example:
    //--- Code in C#
    Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b", RegexOptions.IgnoreCase);   
    string text = "The the quick brown fox  fox jumped over the lazy dog dog.";
    MatchCollection matches = rx.Matches(text);
    foreach (Match match in matches) 
       //--- handling
    //--- Code in MQL4
    Regex *rx = new Regex("\\b(?<word>\\w+)\\s+(\\k<word>)\\b", RegexOptions::IgnoreCase);        
    string text = "The the quick brown fox  fox jumped over the lazy dog dog.";
    MatchCollection *matches = rx.Matches(text);
    IEnumerator<Match*> *en = matches.GetEnumerator();
       Match *match = en.Current();
       //--- handling
    delete rx;
    delete matches;
    delete en;
  4. As can be seen from the above example, the C# syntax allows to put the '@' symbol in front of the strings to ignore all formatting marks in it. In MQL4, this approach is not provided, so all control characters in a regular expression pattern should be explicitly defined.

To learn more about the RegularExpressions for MQL4 and its features, use the provided Tests.mqh expert. It implements numerous examples of regular expression usage, which cover all the main functionality of the library.


Translated from Russian by MetaQuotes Software Corp.
Original code:

Engulfing with RSI Engulfing with RSI

Not really profitable but the code fits on one page.

Previous Candle Hi-Lo Previous Candle Hi-Lo

Previous Candle Hi-Lo is used to check the last candle multi-timeframe position relative to the current price.

Symbol Movement Symbol Movement

This custom indicator will show you 28 pair's daily candle range, High-Low range, candle bullishness or bearishness from real tick market. So you can understand the overall market situation very short time. You can change the default timeframe from Daily to any period and default candle (bar) number from 0 to any previous number. Also you can open the required symbol by clicking the symbol button.

BarNumbers BarNumbers

Displays the number of each bar - both, relative to the most current bar and in absolute terms from the beginning of the chart.