jvance
jvance

Reputation: 551

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

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)

Upvotes: 5

Views: 149

Answers (5)

mayo
mayo

Reputation: 4095

Try this:

Doesn't allow char/space/char combination and starts with a char:

/\b(?!\w\s{1,2}\w+)\w(\w|\s){3}\d{10}/gm

https://regex101.com/r/fF2tR8/2

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89639

Not sure what you are looking for, but perhaps:

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

demo

Upvotes: 2

Mariano
Mariano

Reputation: 6521

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

Upvotes: 4

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627469

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.

Upvotes: 6

ideafixxxer
ideafixxxer

Reputation: 474

I know this is dumb, but must work exactly as required.

^[A-Za-z\d]([A-Za-z\d]{3}|[A-Za-z\d]{2}\s|[A-Za-z\d]\s{2}|\s{3})[\d]{10}$

Upvotes: 2

Related Questions