Skip to main content
Solved

Regex pattern that would validate numbers followed by letters and then numbers


Forum|alt.badge.img+2
  • Newcomer
  • 2 replies

Hello, 

I am trying to build regex to allow only the following type of string: 9 digits followed by 2 letters followed by 4digits.

I came up with ^([0-9]{9})([A-Z]{2})([0-9]{4})$

but DocuSign doesn’t like it and an error message pops up: Save Error Some fields might be out of sync. The regular expression provided is not valid. 

 

other types I have tried and failed:  

^(\d{9}\d([A-Z]{2})\d{4})$  - also failed 

 

^([0-9]{9})((\s)([a-zA-Z]{2})((\s)([0-9]{4})?$ (so this would mean there would be spaces in between numbers and letters

 

I think maybe something is not correct around letter validation?

 

Best answer by JohnSantos

@AgaB - 

Your regular expression is almost correct, but there's a small mistake in it. Instead of using (\s) to match spaces between digits and letters, you should use \s* to match zero or more spaces. Also, you can simplify it a bit. Here's the corrected version:

 

regex

Copy code

^([0-9]{9})\s*([A-Za-z]{2})\s*([0-9]{4})$

In this expression:

  • ^ asserts the start of the string.
  • ([0-9]{9}) matches exactly 9 digits.
  • \s* matches zero or more spaces.
  • ([A-Za-z]{2}) matches exactly 2 letters, either uppercase or lowercase.
  • \s* matches zero or more spaces.
  • ([0-9]{4}) matches exactly 4 digits.
  • $ asserts the end of the string.

This should match the pattern you described: 9 digits followed by 2 letters followed by 4 digits, with optional spaces in between.

View Original
Is this content helpful?

4 replies

Alexandre.Augusto
Docusign Employee
Forum|alt.badge.img+14

Hello, @AgaB 

 

Welcome to the Docusign Community!

 

Have you tried to find an ready to go REGEX mask in the Regex Library site?

https://regexlib.com/

 

Perhaps, you will find one doing what you want or at least, give ideas to do it.

 

Best,

Alexandre


Forum|alt.badge.img+2
  • Author
  • Newcomer
  • 2 replies
  • May 6, 2024

Thanks @Alexandre.Augusto - I used similar sites to create regex pattern with letters. 

I will check out the one you recommended - Thanks,

Docusign has some good examples how to create numbers validations but sadly not letters 

 


JohnSantos
Valued Contributor
Forum|alt.badge.img+18
  • Valued Contributor
  • 975 replies
  • Answer
  • May 6, 2024

@AgaB - 

Your regular expression is almost correct, but there's a small mistake in it. Instead of using (\s) to match spaces between digits and letters, you should use \s* to match zero or more spaces. Also, you can simplify it a bit. Here's the corrected version:

 

regex

Copy code

^([0-9]{9})\s*([A-Za-z]{2})\s*([0-9]{4})$

In this expression:

  • ^ asserts the start of the string.
  • ([0-9]{9}) matches exactly 9 digits.
  • \s* matches zero or more spaces.
  • ([A-Za-z]{2}) matches exactly 2 letters, either uppercase or lowercase.
  • \s* matches zero or more spaces.
  • ([0-9]{4}) matches exactly 4 digits.
  • $ asserts the end of the string.

This should match the pattern you described: 9 digits followed by 2 letters followed by 4 digits, with optional spaces in between.


Forum|alt.badge.img+2
  • Author
  • Newcomer
  • 2 replies
  • May 6, 2024

@JohnSantos  - thanks so much for your feedback!

it works perfectly - I just completed couple of tests, and all works as expected. 

cheers!