Salut, j'ai une chaîne de texte que je dois faire correspondre, et j'ai essayé pat match() mais je ne l'ai pas compris.Le texte est un nom de fichier. J'ai besoin d'identifier tous les fichiers avec un certain modèle :
"AB0007 25092019 0822.txt":
2 majuscules. "AB" en fait toujours.
4 chiffres et un espace
ddMMYYYY (ou juste 8 chiffres) et un espace
4 chiffres
".SMS"
J'ai essayé mais j'ai échoué avec de nombreuses versions, la dernière:
file="AB0007 25092019 0822.txt" // pour test ; finalement ce sera un élément dans une liste de fichiers que je parcours
motif="AB"+"\d{4}"+"\s"+"\d{8}"+"\s"+"\d{4}" ;
pat match(fichier,motif);
Toute aide serait appréciée!
JSL a un expression régulière fonction qui fait des correspondances de regex traditionnelles et une patmatch fonction qui peut utiliser des extraits de regex si vous le souhaitez. Le dernier exemple indique pourquoi vous pourriez vouloir utiliser le patmatch fonction.Notez également le bit supplémentaire de la logique ^...$ ou PatPos(0)...PatRPos(0) pour vous assurer que le nom de fichier entier correspond.
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(fichier, "^AB\d{4}\s\d{8}\s\d{4}\.txt$") = "AB0007 25092019 0822.txt" ;
Pat Match(fichier, motif) = 1 ;
Pat Match(fichier, motif) = 1 ;
Pat Match(fichier, motif) = 1 ;
Pat Match(fichier, motif) = 1 ;
Informat(datepart, "ddmmyyyy") = 25sep2019 ;
L'expression régulière de JMP est construite au-dessus des fonctions de correspondance de modèles ; aucun des deux ne sera plus rapide que l'autre. Le PatRegex traduit la regex en fonctions de correspondance de modèle équivalentes afin que PatMatch fonctionne.