开发者

Regex To Validate Model/Part Numbers

I'm trying to write a regex to validate part or model numbers.

These can con开发者_开发知识库tain letters, numbers, '-', '/' and spaces. They must contain at least 1 number and be between 4 and 20 characters long.

Here are some examples of the strings I want to match:

CVA 620 999
M3094
26250
APL8215/APL8225
1301
02-700401

This is what I have so far

([\w- /]*\d){3,19}

It seems to be working apart from it will match strings such as "This is my model APL8215", I only want it to match the "APL8215" part.

Is there anyway to match model numbers like this using regular expressions?

Any help very much appreciated!


I think this one can respond to your problem :

\b((?=[A-Za-z/ -]{0,19}\d)[A-Za-z0-9/ -]{4,20})\b

It looks for a 4 to 20 chars string composed of [A-Za-z0-9/ -] chars and this string must be "on its own" (\b stands for word boundary). This string must contain at least one number : this is done with a lookahead expression : (?=[A-Za-z/ -]{0,19}\d).

With the following sample :

CVA 620 999
M3094
26250
APL8215/APL8225
1301
1232-1231
02-700401
DGEIVEOCE
cdzjkblcvsz#56464e
siovbsbf~1313/
APL8215/APL8225APL8215/APL8225

I get :

"CVA 620 999" 
"M3094" 
"26250" 
"APL8215/APL8225" 
"1301" 
"1232-1231" 
"02-700401" 
"56464e" 
"1313"
"APL8215/"
"APL8225APL8215/"
"APL8225" 

The last results are explained by the word-boundary values expected : '/' can be word boundary. If you want to solve that problem, you must use a lookbehind before and a lookahead after the main Regex.

Is that what you wanted ?


IMO it's better to make one regex per model number format and
then combine them in one big regex.

Example: r = (modelA_regex)|(modelB_regex)|(modelC_regex)


This is as close as I can get:

(?=.*\d)[\w\d\- ]{4,20}

Unfortunately it doesn't work with the example This is my model APL8215 because the rules are pragmatic enough to match This is my model APL as a valid part number before matching the APL8215 part.


Unfortunately because of the flexibility of your regex, this will of course match strings as you have specified above. If you could restrict your criteria further, for example to only include capitalised letters, then you would be able to pick up these codes with a regex such as:

[A-Z0-9- ]{4,20}


Since it seems not to be possible to group all model numbers under the same umbrella, I'd use more than one regexp:

  • CVA xxx xxx
  • Mxxxx
  • xxxxx
  • APLxxxx

where x is a digit (from your example) and so on. Once you have extracted the relevant subset of regexp, you can then refine your parsing or concat them into the same output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜