2026-03-27 09:40:26 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| StrSimpleMatch.mqh |
|
| | | //| Copyright 2026, Niquel Mendoza. |
|
| | | //| https://www.mql5.com/es/users/nique_372 |
|
| | | //+------------------------------------------------------------------+
|
| | | #property copyright "Copyright 2026, Niquel Mendoza."
|
| | | #property link "https://www.mql5.com/es/users/nique_372"
|
| | | #property strict
|
| | |
|
| | | #ifndef MQLARTICLES_UTILS_STRMATCH_STRSIMPLEMATCH_MQH
|
| | | #define MQLARTICLES_UTILS_STRMATCH_STRSIMPLEMATCH_MQH
|
| | |
|
| | |
|
2026-06-03 22:08:10 -05:00 | | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | #include "..\\\..\\..\\TsnTables\\Src\\Tables.mqh"
|
| | |
|
| | |
|
2026-03-27 09:40:26 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | class CSimpleStringMatch
|
| | | {
|
| | | public:
|
| | | CSimpleStringMatch(void) {}
|
| | | ~CSimpleStringMatch(void) {}
|
| | |
|
| | | //---
|
| | | // Comprubea si un string sigue cierto patron
|
| | | // el string pattern puede tener los siguientes caracteres:
|
| | | // - ?: Indica que debe de haber un caracter (Cualquier cosa)
|
2026-04-30 15:56:25 -05:00 | | | // - *: Indica que debe de haber todo tipo de caracteres hasta cierto valor (mas lejano)
|
2026-03-27 09:40:26 -05:00 | | | // - ^: Indica que debe de haber un caracter (solo digitos del 0 al 9)
|
2026-04-30 15:56:25 -05:00 | | | // - ~: Indica que debe de haber todo tipo de caracteres hasta cierto valor (mas cercano)
|
2026-03-27 09:40:26 -05:00 | | | // Ahora mencionar que todos esos patrones se pueden combinar para formar el string pattern eg "*.png"
|
| | | // Otra cosa importante es que si en el string pattern luego del * hay un caracter este se toma como "finalizador" asi que "**"
|
2026-04-30 15:56:25 -05:00 | | | // Se buscaria strings de este tipo "hola*" y el pattenr "**"
|
2026-03-27 09:40:26 -05:00 | | | // Retorna true si el string analizado coindice con el patron de lo contrario false
|
2026-04-30 15:56:25 -05:00 | | |
|
2026-03-27 09:40:26 -05:00 | | | static bool SimpleMatchInString(const string& str, const string& pattern);
|
| | | };
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | static bool CSimpleStringMatch::SimpleMatchInString(const string& str, const string& pattern)
|
| | | {
|
2026-04-30 15:56:25 -05:00 | | | //---
|
| | | if(pattern == "*" || pattern == "~")
|
| | | return true;
|
| | |
|
| | | //---
|
2026-03-27 09:40:26 -05:00 | | | const int len = StringLen(str);
|
| | | const int lenp = StringLen(pattern);
|
| | | int pos_str = 0, pos_p = 0, pi = 0;
|
| | | while(pos_str < len)
|
| | | {
|
| | | if(pos_p < lenp && str[pos_str] == pattern[pos_p])
|
| | | {
|
| | | pos_p++;
|
| | | pi++;
|
| | | pos_str++;
|
| | | continue;
|
| | | }
|
| | |
|
2026-04-30 15:56:25 -05:00 | | | //--- Todo (astericos) - Mas lejano
|
| | | // "\\Hola\\a"
|
| | | // "*\\abc*a"
|
2026-03-27 09:40:26 -05:00 | | | if(pattern[pos_p] == '*')
|
2026-04-30 15:56:25 -05:00 | | | {
|
| | | if(pos_p < lenp - 1)
|
| | | {
|
| | | pos_p++;
|
| | | int k = pos_str;
|
| | | int max = k;
|
| | |
|
| | | while(k < len)
|
| | | {
|
| | | if(str[k] == pattern[pos_p])
|
| | | {
|
| | | max = k;
|
2026-09-02 15:54:33 -05:00 | | | break;
|
2026-04-30 15:56:25 -05:00 | | | }
|
| | | k++;
|
| | | }
|
| | | pos_str = max == pos_str ? k : max;
|
| | | }
|
| | | else
|
| | | pos_str = len;
|
| | |
|
| | | pi++;
|
| | | continue;
|
| | | }
|
| | |
|
| | | //--- Todo (~) - Mas Cercano
|
| | | if(pattern[pos_p] == '~')
|
2026-03-27 09:40:26 -05:00 | | | {
|
| | | if(pos_p < lenp - 1)
|
| | | {
|
| | | pos_p++;
|
| | | while(pos_str < len)
|
| | | {
|
| | | if(str[pos_str] == pattern[pos_p])
|
| | | {
|
| | | break;
|
| | | }
|
| | | pos_str++;
|
| | | }
|
| | | }
|
| | | else
|
| | | pos_str = len;
|
| | |
|
| | | pi++;
|
| | | continue;
|
| | | }
|
| | |
|
| | | //--- Cualquier caracter
|
| | | //??
|
| | | //aa
|
| | | if(pattern[pos_p] == '?')
|
| | | {
|
| | | pos_p++;
|
| | | pos_str++;
|
| | | const bool superated_p = pos_p >= lenp;
|
| | | if(pos_str < len && superated_p)
|
| | | return false; //
|
| | | pi++;
|
| | | if(superated_p)
|
| | | break;
|
| | | continue;
|
| | | }
|
| | |
|
| | | //--- Solo numeros (0-9)
|
| | | if(pattern[pos_p] == '^')
|
| | | {
|
| | | pos_p++;
|
| | |
|
| | | if(!IsDigit(str[pos_str]))
|
| | | return false; // Este caracter no es un digito
|
| | |
|
| | | pos_str++;
|
| | | const bool superated_p = pos_p >= lenp;
|
| | | if(pos_str < len && superated_p)
|
| | | return false; //
|
| | |
|
| | | pi++;
|
| | |
|
| | | if(superated_p)
|
| | | break;
|
| | | continue;
|
| | | }
|
| | | #undef IsDigit // IsDigit
|
| | |
|
| | | //--- Fallo
|
| | | //Print("no");
|
| | | return false;
|
| | | }
|
| | | //Print("pi: ", pi, " lep: ", lenp);
|
2026-04-30 15:56:25 -05:00 | | | return pi == lenp;
|
2026-03-27 09:40:26 -05:00 | | | }
|
| | | //+------------------------------------------------------------------+
|
2026-04-30 15:56:25 -05:00 | | | #endif // MQLARTICLES_UTILS_STRMATCH_STRSIMPLEMATCH_MQH
|