JCKE
JCKE

Reputation: 394

Regex: Match 'no characters' between strings

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:

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:

Upvotes: 0

Views: 214

Answers (2)

Rahul
Rahul

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.

Regex101 Demo

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:

  1. (?=.*[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.

  2. [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

emnoor
emnoor

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

Related Questions