Ciao, ho una stringa di testo che devo abbinare e ho provato pat match() ma non ne ho la comprensione.Il testo è un nome file. Ho bisogno di identificare tutti i file con un certo schema:
"AB0007 25092019 0822.txt":
2 lettere maiuscole. "AB" in realtà sempre.
4 cifre e uno spazio
ggMMAAAA (o solo 8 cifre) e uno spazio
4 cifre
".TXT"
Ho provato ma non ci sono riuscito con molte versioni, l'ultima:
file="AB0007 25092019 0822.txt" // per il test; alla fine sarà un elemento in un elenco di file che scorro
pattern="AB"+"\d{4}"+"\s"+"\d{8}"+"\s"+"\d{4}";
pat match(file,modello);
Qualsiasi aiuto sarebbe apprezzato!
JSL ha un espressione regolare funzione che esegue le tradizionali corrispondenze regex e a patmatch funzione che può utilizzare frammenti di regex se lo si desidera. L'ultimo esempio suggerisce perché potresti voler usare il file patmatch funzione.Si noti anche il bit in più della logica ^...$ o PatPos(0)...PatRPos(0) per assicurarsi che l'intero nome file sia stato abbinato.
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(file, "^AB\d{4}\s\d{8}\s\d{4}\.txt$") = "AB0007 25092019 0822.txt";
Pat Match(file, modello) = 1;
Pat Match(file, modello) = 1;
Pat Match(file, modello) = 1;
Pat Match(file, modello) = 1;
Informat(datepart, "ddmmyyyy") = 25Sep2019;
La regex di JMP si basa sulle funzioni di pattern matching; nessuno dei due sarà più veloce dell'altro. IL Pat Regex funzione traduce la regex in funzioni di corrispondenza del modello equivalenti in modo che PatMatch funzioni.