Regular Expressions in MQL5 for working with regular expressions - MetaTrader 5 Library - MT4/MT5 Resources





Microsoft Corporation. Source code taken from .Net Framework 4.6.1 .
Note: This library is available for MetaTrader 5 build 1285 and higher.
Extract the archive to the terminal data folder .
Library code is located at:
Sample test scripts can be found at
The following is the translation of regular expressions in .Net Framework 4.6.1.
To use the library, include the Regex.mqh file in the "\MQL5\Include\RegularExpressions\" directory into your code.
The library provides several illustrative examples that also serve as test cases. All samples are taken from the official website of Microsoft Corporation , and they vividly demonstrate the main differences with regular expressions in C# and the characteristics of their use in MQL5.
Here are more details about the Regular Expressions MQL5 porting library package:
Regular expression parameters in the file regexoptions.mqh :
Regular expressions using MQL5:
//--- C# code regular expression rx = new regular expression ( @"\b(?<word>\w+)\s+(\k<word>)\b" , RegexOptions.IgnoreCase); string text = "The quick brown fox jumped over the lazy dog." ; MatchCollection matches = rx.Matches(text); foreach (match match in match) { ; MatchCollection *matches = rx.Matches(text); IEnumerator*en = matches.GetEnumerator(); although (en.MoveNext()) { Match *match = en.Current(); //--- handle } delete receive; delete match; delete en; Regular Expression:: ClearCache ();
Example using MQL5 regular expressions:
As an example of regular expressions, consider their application to parse transaction history downloaded from a terminal in the form of html files.
To do this, create an EA with a single input parameter of type "String", which will be the sandbox file :

The document contains two main tables: "Orders" and "Transactions".
Create a regular expression to parse the file:
Regular expression *rgx= new regular expression ( “(>)([^<>]*)(<)” );
Consider this regular expression:
Next, read the file line by line and get all matches corresponding to this regular expression:
string string = file read string (m_handel); MatchCollection *matches=rgx.Matches(str);
The strings of the html file, which are the entries (tuples) of the "Orders" and "Deals" tables, will have the most such matches, 23 and 27 matches respectively. So the only thing left to do is to extract all the necessary information from these strings.
For the "Orders" table:
if (match.Count()== 23 ) { String in [ 11 ]; for ( integer i= 0 ,j= 1 ;i< 11 ;i++,j+= 2 ) { In [i]=StringSubstr(matches [j].Value(), 1 , StringLen(matches [j].Value())- 2 ); } m_list1.Add( new order record( in )); }
The number of confirmed matches is 23, so the entries processed are from the "Orders" table. Create an array of strings as the initial representation of our entries. Create an iteration over all odd matches and get the values of these matches using the match[j][.Value() method, trimming the first and last characters of each match, corresponding to the characters ">" and "<". Each formatted match is written to the previously declared batch. Afterwards create an instance of the OrderRecord(in) class which represents a single entry of the "Orders" table and add it to the m_list1 list. This list will explain the "Order" table.
The "Deals" table will be handled in a similar manner:
if (match.Count()== 27 ) { String in [ 13 ]; for ( integer i= 0 ,j= 1 ;i< 13 ;i++,j+= 2 ) { In [i]=StringSubstr(matches [j].Value(), 1 , StringLen(matches [j].Value())- 2 ); } m_list2.Add( new transaction record( in )); }
Here m_list2 is a pointer to a list of transactions class. It represents the "Transaction" table.
Both lists are trade history classes. This class is a representation of the entire original html file. Additionally, it allows applying simple filters to the "Orders" and "Trades" tables using the following methods: FindAllOrders(const int columnIndex,const T value) and FindAllDeals(const int columnIndex,const T value) .
Create a simple graphical interface in the EA to demonstrate these functions:

When using this form, select the desired table and select columns and their values to filter the table. Press the Find button and the filtered table will be displayed below, along with some statistical information. This save button stores the currently displayed table into a csv file. The saved file will also be located in the sandbox and have the name results.csv .
To learn more about MQL5 regular expressions and their functionality, use the test provided by .mqh Expert. It implements many regular expression usage examples, covering all the main features of the library.

Attachment download
📎 TableListView.mqh (60.38 KB)
📎 Tests.mq5 (139.55 KB)
📎TradeHistoryParsing.mq5 (55.11 KB)
📎 Array.mqh (22.32 KB)
📎 IComparable.mqh (4.98 KB)
📎 dynamicmatrix.mqh (22 KB)
📎 wrappers.mqh (16.99 KB)
📎 Collection.mqh (12.54 KB)
📎 Dictionary.mqh (42.58 KB)
📎 EqualityComparer.mqh (8.65 KB)
📎 ICollection.mqh (6.15 KB)
📎 IComparer.mqh (6.62 KB)
📎 IDictionary.mqh (6.88 KB)
📎 IEnumerable.mqh (5.31 KB)
📎 IEnumerator.mqh (10.39 KB)
📎 IEqualityComparer.mqh (4.62 KB)
📎 IList.mqh (5.7 KB)
📎 KeyValuePair.mqh (6.34 KB)
📎 LinkedList.mqh (28.3 KB)
📎 List.mqh (38.62 KB)
📎 TimeSpan.mqh (31.7 KB)
📎 TimeSpanFormat.mqh (30.31 KB)
📎CharUnicodeCategory.txt (23.5 KB)
📎 Regex.mqh (58.05 KB)
📎 RegexBoyerMoore.mqh (18.01 KB)
📎 RegexCapture.mqh (7.86 KB)
📎 RegexCaptureCollection.mqh (10.25 KB)
📎 RegexCharClass.mqh (97.97 KB)
📎 RegexCode.mqh (22.25 KB)
📎 RegexFCD.mqh (31.22 KB)
📎 RegexGroup.mqh (8.72 KB)
📎 RegexGroupCollections.mqh (11.25 KB)
📎 RegexInterpreter.mqh (49.66 KB)
📎 RegexMatch.mqh (22.94 KB)
📎 RegexMatchCollection.mqh (11.04 KB)
📎 RegexNode.mqh (32.45 KB)
📎 RegexOptions.mqh (14.82 KB)
📎 RegexParser.mqh (93.26 KB)
📎 RegexReplacement.mqh (20.83 KB)
📎 RegexRunner.mqh (31.31 KB)
📎 RegexTree.mqh (8.47 KB)
📎 RegexWriter.mqh (24.52 KB)
Source: MQL5 #15242
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •