4

I am uploading a file and I need to check whether the filename belongs to a particular pattern or not.

Example: AL-13-Annual DSH Audit.pdf

The first two characters "AL" can be anything but will be only 2 Uppercase characters, followed by a dash(-), followed by a 2 digit number, followed by a dash(-), followed by the rest('Annual DSH Audit.pdf').

This is what I have done so far.

[A-Z]{2}[\-][0-9]{2}[\-][A]{1}[nnual]{5}\s[DSH]{3}\s[A]{1}[udit]{4}\.[pdf]{3}

Please advice. Not sure how to proceed further to simplify this.

chazsolo
  • 7,873
  • 1
  • 20
  • 44
xin
  • 61
  • 4
  • 1
    If you want to match a dash after the two upper case characters, then that's exactly what you need to add: `[A-Z]{2}-`. Add the characters you mentioned in your description. If you know how to match two uppercase characters, I assume you know how to match two digits. So what exactly is the problem? – Felix Kling Mar 12 '19 at 19:14
  • Will "Annual DSH Audit" always be present, or can that be any string? I get the impression from your question that this is the case, just checking. – skwidbreth Mar 12 '19 at 19:15
  • `/^[A-Z]{2}-\d{2}-/` – ibrahim mahrir Mar 12 '19 at 19:15
  • @FelixKling fixed the regex. Anyway to simplify this? – xin Mar 12 '19 at 19:19
  • Do all the filenames have to have the substring `"Annual DSH Audit.pdf"` in them? – ibrahim mahrir Mar 12 '19 at 19:20
  • `[...]` denote character groups, i.e. "match any one of these characters". Thus `[nnual]` is the same as `[nual]` and `[nnual]{5}` would match `nnnnn` or `nnuul`. If you you literally want to match a specific sequence of characters, then you just write them out as is. Don't use a character group: `Annual\sDSH\sAudit` – Felix Kling Mar 12 '19 at 19:24
  • Yeah, looks like the files he uploads should have the same string with same cases – arunmmanoharan Mar 12 '19 at 19:25
  • everything past the last `.` ? Any reason that would not work? – Ronnie Royston Mar 12 '19 at 19:26
  • You should check out this website for testing regex patterns, it's very useful: https://regexr.com/4a34v – Chris Barr Mar 12 '19 at 19:34

3 Answers3

2

^[A-Z]{2}-\d{2}-Annual\sDSH\sAudit\.pdf$

*Edited to meet your requirements

Link to demo - https://regexr.com/4a344

Chris Sandvik
  • 1,787
  • 9
  • 19
0

const matchesFormat = str => /^[A-Z]{2}-\d{2}-.+\.pdf$/.test(str);

console.log( matchesFormat('AL-13-Annual DSH Audit.pdf') ); // true
console.log( matchesFormat('ZZ-00-test.pdf') );             // true
console.log( matchesFormat('AL-13-Annual DSH Audit.jpg') ); // false
console.log( matchesFormat('AL-13Annual DSH Audit.pdf') );  // false
console.log( matchesFormat('K-13-Annual DSH Audit.pdf') );  // false
console.log( matchesFormat('AL-2-Annual DSH Audit.pdf') );  // false

and if you need to also match the rest Annual DSH Audit.pdf than:

https://regex101.com/r/NNtx6o/1

const isAuditPDF = str => /^[A-Z]{2}-\d{2}-Annual\sDSH\sAudit\.pdf$/.test(str);

console.log( isAuditPDF('AL-13-Annual DSH Audit.pdf') ); // true
console.log( isAuditPDF('ZZ-00-test.pdf') );             // false
console.log( isAuditPDF('AL-1-Annual DSH Audit.pdf') );  // false
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
0

Try

^[A-Z]{2}-\d\d-

let t= /^[A-Z]{2}-\d\d-/.test('AL-13-Annual DSH Audit.pdf'); 
console.log(t);
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345