Hola, tengo una cadena de texto que necesito hacer coincidir, y he estado intentando pat match () pero no tengo la comprensión.El texto es un nombre de archivo. Necesito identificar todos los archivos con un cierto patrón:
"AB0007 25092019 0822.txt":
2 letras mayúsculas. "AB" en realidad siempre.
4 dígitos y un espacio
ddMMYYYY (o solo 8 dígitos) y un espacio
4 dígitos
".TXT"
Lo he estado intentando pero he fallado con muchas versiones, la última:
file="AB0007 25092019 0822.txt" // para pruebas; eventualmente será un elemento en una lista de archivos por la que recorreré
patrón="AB"+"\d{4}"+"\s"+"\d{8}"+"\s"+"\d{4}";
coincidencia de palmaditas (archivo, patrón);
¡Cualquier ayuda sería apreciada!
JSL tiene un expresiones regulares función que hace coincidencias de expresiones regulares tradicionales y un patmatch función que puede usar fragmentos de expresiones regulares si lo desea. El último ejemplo sugiere por qué es posible que desee utilizar el patmatch función.Observe también el bit adicional de la lógica ^...$ o PatPos(0)...PatRPos(0) para asegurarse de que coincida todo el nombre del archivo.
file = "AB0007 25092019 0822.txt"; // for testing; eventually it will be an item in a file list that I cycle through
// decide if using (1) pure regex,
Show( Regex( file, "^AB\d{4}\s\d{8}\s\d{4}\.txt$" ) ); // "AB0007 25092019 0822.txt", or missing (.) if not matched
// or (2) pure pattern matching,
digit = Pat Any( "0123456789" );
pattern = Pat Pos( 0 ) + "AB" + Pat Repeat( digit, 4, 4 ) + " " + Pat Repeat( digit, 8, 8 ) + " " + Pat Repeat( digit, 4, 4 ) + ".txt" + Pat R Pos( 0 );
Show( Pat Match( file, pattern ) ); // rc is 1, it matches, 0 if fails to match
// or (3a) mix of both
pattern = Pat Pos( 0 ) + "AB" + Pat Regex( "\d{4}" ) + Pat Regex( "\s" ) + Pat Regex( "\d{8}" ) + Pat Regex( "\s" ) + Pat Regex( "\d{4}" ) + ".txt" + Pat R Pos( 0 );
Show( Pat Match( file, pattern ) ); // rc is 1, it matches, 0 if fails to match
// or collapse the mix (3b) a bit
pattern = Pat Pos( 0 ) + "AB" + Pat Regex( "\d{4}\s\d{8}\s\d{4}" ) + ".txt" + Pat R Pos( 0 );
Show( Pat Match( file, pattern ) ); // rc is 1, it matches, 0 if fails to match
// why you might choose (2) or (3a) -- you can capture substrings like the date part using the >> operator:
digit = Pat Any( "0123456789" );
pattern = Pat Pos( 0 ) + "AB" + Pat Repeat( digit, 4, 4 ) + " " + Pat Repeat( digit, 8, 8 ) >> datepart + " " + Pat Repeat( digit, 4, 4 ) + ".txt" + Pat R Pos( 0 );
Show( Pat Match( file, pattern ),informat(datepart,"ddmmyyyy") ); // rc is 1, Informat(datepart, "ddmmyyyy") = 25Sep2019;Regex(archivo, "^AB\d{4}\s\d{8}\s\d{4}\.txt$") = "AB0007 25092019 0822.txt";
Pat Match(archivo, patrón) = 1;
Pat Match(archivo, patrón) = 1;
Pat Match(archivo, patrón) = 1;
Pat Match(archivo, patrón) = 1;
Informat(datepart, "ddmmaaaa") = 25Sep2019;
La expresión regular de JMP se basa en las funciones de coincidencia de patrones; ninguno será más rápido que el otro. El PatRegex La función traduce la expresión regular en funciones equivalentes de coincidencia de patrones para que PatMatch funcione.