I am trying to write a regex using C # /. Net, which matches 1-4 letter characters, followed by spaces, followed by 10 digits. Trap is the number of spaces plus the number of alphanumeric characters should be 4, and the spaces should follow the alphanumeric characters, and not be marked.
I have a complete loss on how to do this. I can do ^[A-Za-z\d\s]{1,4}[\d]{10}$ , but this allows you to skip spaces somewhere in the first four characters. Or I could do ^[A-Za-z\d]{1,4}[\s]{0,3}[\d]{10}$ to save space together, but that would allow more than just four characters before the 10-digit number.
Valid: A12B1234567890 AB1 1234567890 AB 1234567890
Invalid: AB1 1234567890 (more than 4 characters before the numbers) A1B1234567890 (less than 4 characters before the numbers) A1 B1234567890 (space amidst the first 4 characters instead of at the end)
source share