Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to match a group of alphanumerics followed by a group of spaces, making a fixed total of characters

Tags:

c#

.net

regex

I'm trying to write a regular expression using C#/.Net that matches 1-4 alphanumerics followed by spaces, followed by 10 digits. The catch is the number of spaces plus the number of alphanumerics must equal 4, and the spaces must follow the alphanumerics, not be interspersed.

I'm at a total loss as to how to do this. I can do ^[A-Za-z\d\s]{1,4}[\d]{10}$, but that lets the spaces fall anywhere in the first four characters. Or I could do ^[A-Za-z\d]{1,4}[\s]{0,3}[\d]{10}$ to keep the spaces together, but that would allow more than a total of 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)

like image 529
jvance Avatar asked Sep 09 '15 20:09

jvance


2 Answers

You can force the check with a look-behind (?<=^[\p{L}\d\s]{4}) that will ensure there are four allowed characters before the 10-digits number:

^[\p{L}\d]{1,4}\s{0,3}(?<=^[\p{L}\d\s]{4})\d{10}$
                      ^^^^^^^^^^^^^^^^^^^^  

See demo

If you do not plan to support all Unicode letters, just replace \p{L} with [a-z] and use RegexOptions.IgnoreCase.

like image 139
Wiktor Stribiżew Avatar answered Nov 03 '22 00:11

Wiktor Stribiżew


Here's the regex you need:

^(?=[A-Za-z0-9 ]{4}\d{10}$)[A-Za-z0-9]{1,4} *\d{10}$

It uses a lookahead (?= ) to test if it's followed by 4 chars, either alnum or space, and then it goes back to where it was (the beggining of string, not consuming any chars).

Once that condition is met, the rest is a expression quite similar to what you were trying ([A-Za-z0-9]{1,4} *\d{10}).

Online tester

like image 31
Mariano Avatar answered Nov 03 '22 00:11

Mariano