1

I have a filename that will be something along the lines of this:

Annual-GDS-Valuation-30th-Dec-2016-082564K.docx

It will contain 5 numbers followed by a single letter, but it may be in a different position in the file name. The leading zero may or may not be there, but it is not required.

This is the code I come up with after checking examples, however SelectedFileClientID is always null

var SelectedFileClientID = files.match(/^d{5}\[a-zA-Z]{1}$/);

I'm not sure what is it I am doing wrong.

Edit:

The 0 has nothing to do with the code I am trying to extract. It may or may not be there, and it could even be a completely different character, or more than one, but has nothing to do with it at all. The client has decided they want to put additional characters there.

10
  • Maybe /\b0*\d{5}[a-zA-Z]\b/ or /(^|\D)0*\d{5}[a-zA-Z](?![a-zA-Z])/? I doubt a mere /\d{5}[a-zA-Z]/ will work right here. Commented Jan 24, 2017 at 20:48
  • Another example that included 2 letters and 6 numbers didn't have anything like this. Commented Jan 24, 2017 at 20:49
  • Please, show us all examples Commented Jan 24, 2017 at 20:49
  • 1
    What is files? Is it a string, or an array of strings? Commented Jan 24, 2017 at 20:52
  • 1
    Ok, I see my third regex from the initial comment works for you. Posted as an answer. Commented Jan 24, 2017 at 21:10

4 Answers 4

1

There are at least 3 issues with your regex: 1) the pattern is enclosed with anchors, and thus requires a full string match, 2) the d matches a letter d, not a digit, you need \d to match a digit, 3) a \[ matches a literal [, so the character class is ruined.

Use

/\d{5}[a-zA-Z]/

Details:

  • \d{5} - 5 digits
  • [a-zA-Z] - an ASCII letter

JS demo:

var s = 'Annual-GDS-Valuation-30th-Dec-2016-082564K.docx';
var m = s.match(/\d{5}[a-zA-Z]/);
console.log(m[0]);

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I needed. I mentioned about the 0 because it may or may not be there, and has nothing to do with the code I am trying to extract. So this works perfectly. I also struggle with RegEx and syntax as I'm a bit dyslexic. Many thanks for your help on this one.
0

All right, there are a few things wrong...

var matches = files.match(/\-0?(\d{5}[a-zA-Z])\.[a-z]{3,}$/);
var SelectedFileClientID = matches ? matches[1] : '';

So:

  1. First, I get the matches on your string -- .match()
  2. Then, your file name will not start with the digits - so drop the ^
  3. You had forgotten the backslash for digits: \d
  4. Do not backslash your square bracket - it's here used as a regular expression token
  5. no need for the {1} for your letters: the square bracket content is enough as it will match one, and only one letter.

Hope this helps!

4 Comments

Thanks. The 0 should be completely ignored. The client is for some reason adding that into the file name for no reason, but insists the code should be able to ignore it. It will always be in this format 12345A
I see. Ok, I've modified the regular expression to reflect this fact. Also, I had forgotten your file name will end with an extension (.docx) so I've added that to the regex too, allowing for an extension of at least 3 lowercase letters.
I have managed to get what I want with just this /\d{5}[a-zA-Z]/. The file name could be in any format at all. The client insists that should be able to set the file name as "i32i4ui43gt00012345L.aaa.bbb.cc" if they wanted to, and it will still pick up the 12345L as the file ID. Thanks for your help.
Ok, fair enough. But without any delimiters to the string you are looking for, this will not be detected correctly: "i32456ui43gt00012345L.aaa.bbb.cc" - your regex will grab the first occurrence "32456u"
0

Try this pattern , \d{5}[a-zA-Z]

Comments

0

Try - 0?\d{5}[azA-Z] As you mentioned 0 may or may not be there. so 0? will take that into account.

Alternatively it can be done like this. which can match any random character.

(\w+|\W+|\d+)?\d{5}[azA-Z]

1 Comment

The 0 has nothing to do with the code I am trying to extract, just the client has decided they can put an additional character there which has nothing to do with it, because "the code should be able to work that out". Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.