Reputation: 394
I have to verify that strings match the following format before the first whitespace (if there is one):
To give examples, the following are valid:
1234
Abc123456DeF
1234 blah+
XyZ01234
I'm having trouble avoiding this case however: 123a+b blah
So far I have (^\w{0,3}\d{4}\w{0,3})\s*
but the problem lies in making sure a non-letter isn't caught in the first section.
I can see a couple solutions:
Run regex twice, first getting the string up to the first whitespace ([^\s]+)
then apply regex again to that making sure it ends in up to 3 letters (^\w{0,3}\d{4}\w{0,3}$)
. This is what I do now, but surely there's a way to do this in one expression - I just can't figure out how
Make sure no non-letters exist between the (potential) 3 trailing letters and the (potential) whitespace. (^\w{0,3}\d{4}\w{0,3}no non-letters)\s*
I've tried negative lookahead (?!.*)
but that doesn't seem to do anything.
Upvotes: 0
Views: 214
Reputation: 2748
This regex satisfy your specifications.
Regex: ^\w{0,3}\d{4,}\w{0,3}\s?$
Explanation: According to your specifications.
\w{0,3}?
Up to 3 leading letters
\d{4,}
At least 4 consecutive digits
\w{0,3}?
Up to 3 trailing letters
I have to verify that strings match the following format before the first whitespace (if there is one):
\s?
hence an optional space.
Note:- I am keeping this as stroked out because there were many shortcomings pointed out in comments. So to maintain the context of comments.
Solution:
Like I said in my comment.
@JCK: Problem is . . even whitespace is optional. Thus making it difficult to differentiate between first and second part.
Now employing a lookahead solves this problem. Complete regex goes like this.
Regex: ^(?=.*[0-9]{4,}[A-Za-z]{0,3}(?:\s|$))[A-Za-z]{0,3}[0-9]{4,}[A-Za-z]{0,3}\s*?(?:\S*\s*)*$
Explanation:
(?=.*[0-9]{4,}[A-Za-z]{0,3}(?:\s|$))
This positive lookahead makes sure that the first part defined by your specifications is matched. It looks for mentioned specs and either a \s
or $
i.e end of string. Thus matching the first part.
[A-Za-z]{0,3}[0-9]{4,}[A-Za-z]{0,3}\s*?(?:\S*\s*)*
Rest of the regex is as per the specifications.
Check by entering strings one by one.
Upvotes: 1
Reputation: 2708
Regex: (^[A-Za-z]{0,3}\d{4,}[A-Za-z]{0,3})(?:$|\s+)
\w
is same as [A-Za-z0-9_]
, so to match just letters you should use [A-Za-z]
.
(?:$|\s+)
matches end of string or at least one whitespace (hence ignoring the rest of the string).
Upvotes: 0