Hallo, ich habe eine Textzeichenfolge, die ich abgleichen muss, und ich habe pat match() ausprobiert, habe aber keine Ahnung davon.Der Text ist ein Dateiname. Ich muss alle Dateien mit einem bestimmten Muster identifizieren:
„AB0007 25092019 0822.txt“:
2 Großbuchstaben. „AB“ eigentlich immer.
4 Ziffern und ein Leerzeichen
ddMMYYYY (oder nur 8 Ziffern) und ein Leerzeichen
4 Ziffern
".txt"
Ich habe es versucht, bin aber bei vielen Versionen gescheitert, zuletzt:
file="AB0007 25092019 0822.txt" // zum Testen; Irgendwann wird es ein Element in einer Dateiliste sein, die ich durchlaufe
pattern="AB"+"\d{4}"+"\s"+"\d{8}"+"\s"+"\d{4}";
pat match(file,pattern);
Jede Hilfe wäre dankbar!
JSL hat eine Regex Funktion, die herkömmliche Regex-Übereinstimmungen durchführt, und a Patmatch Funktion, die bei Bedarf Regex-Schnipsel verwenden kann. Das letzte Beispiel gibt Hinweise, warum Sie das verwenden möchten Patmatch Funktion.Beachten Sie auch das zusätzliche Bit der Logik ^...$ oder PatPos(0)...PatRPos(0), um sicherzustellen, dass der gesamte Dateiname übereinstimmt.
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(Datei, Muster) = 1;
Pat Match(Datei, Muster) = 1;
Pat Match(Datei, Muster) = 1;
Pat Match(Datei, Muster) = 1;
Informat(datepart, "ddmmyyyy") = 25Sep2019;
Der reguläre Ausdruck von JMP basiert auf den Mustervergleichsfunktionen. Keiner wird schneller sein als der andere. Der PatRegex Die Funktion übersetzt den regulären Ausdruck in äquivalente Mustervergleichsfunktionen, sodass PatMatch funktioniert.