0

Hi I need a regular expression which is accepting the following strings:

[A-Z]-[A-Z]{3-5}[0-9]{2-4}

for example X-ABC123 or Y-AB1234

The problem now is that the total length of the string on the right side of the hyphen must always be 5 chars in length. Is there a chance to check that with regular expressions only?

2
  • @PrinceJohnWesley I deleted my comment but thanks anyway :D Commented Nov 9, 2011 at 9:52
  • your selection rule and your example don't match, both of your examples have 6 chars after the - and the number of letters and digests you require can only be 5 if there is 3 and 2, may be you should be a bit more specific about what you need so one of the great answers you have gotten can be tailored more for your need Commented Nov 9, 2011 at 10:04

3 Answers 3

1

Just add this after the hyphen :

/(?=[A-Z\d]{5}$)/

Resulting in :

/^[A-Z]-(?=[A-Z\d]{5}$)[A-Z]{3,5}[0-9]{2,4}/

This assumes that your input strings are the strings you posted.

X-ABC123 -> fails
Y-AB1234 -> fails
A-ABD12  -> matches
A-ABV111 -> fails

If the string is part of another string you can replace the $ anchor with \s|$ for example.

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

6 Comments

why (?=[A-Z\d]{0,5}$) instead of (?=[A-Z\d]{5,5}$)?
@FailedDev or replace the $ anchor by \b a word boundary
Why {0,5} when just {5} is correct: must always be 5 chars in length (not up to 5)
@PrinceJohnWesley Only {5} is enough, {5,5} is not needed
@PrinceJohnWesley +0.5 see updated answer. :D. Not {5,5} rather {5}.
|
0

First the problems in your regex

The quantifier is {3,5} and not {3-5} (this would match literally "{3-5}")

You want 3 to 5 letters and 2 to 4 digits and in total 5 letters and digits ==> the only valid combination is then 3 letters followed by 2 digits.

In general you can use a positive lookahead for this

^[A-Z]-(?=.{5}$)[A-Z]{3,5}[0-9]{2,4}$

See it here on Regexr

The (?=.{5}$) part is just looking, if there are from its position to the end ($) 5 characters.

But as said before, if the 3-5 and 2-4 and overall 5 is valid you can just do

^[A-Z]-[A-Z]{3}[0-9]{2}$

6 Comments

minor improvement. Since you are using the lookahead you might as well fail it there if the characters matching are not [A-Z\d] instead of ".".
@FailedDev for the lookahead its just fine to use . for a length check. The wanted characters are then matched by the following pattern.
Yes but you lose efficiency. A-@#$#@ matches in the lookahead but then fails. You could have failed it before the actual capture.
@FailedDev: I think you're getting a little carried away, there. :-)
@AlanMoore Hm. Maybe I am but wouldn't then lookahead have to evaluate first before whatever comes next? What I mean is, wouldn't the overall regex fail at the moment the lookahead fails?
|
0

I think it's definitely possible, from a language theory point of view. Just group it and add the constraint :

I just need to know which language is specifying the regex but something like this :

[A-Z]-(^[A-Z][A-Z0-9]4)

I had the feeling you wanted the right part to start with one char for sure, then either chars or numbers

1 Comment

That doesn't look like any regex flavor I'm familiar with.

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.