简体   繁体   English

正则表达式匹配0-2个字符,后跟4位

[英]Regular expression to match 0-2 characters followed by 4 digits

I'd like to make a regular exp for C#. 我想为C#做一个常规的exp。 The 1st two letters are characters which are optional and then 4 digits which are mandatory. 前两个字母是可选字符,然后是必填的4位数字。 as: 如:

4584
0259
0015
G3227
G3277
G4018
G3737
G3737
G3277
GU4444
GU4444
G3277
G3277
G3988
C3737
G3227
G3227

I suggest this: 我建议这样:

\b\p{L}{0,2}\d{4}\b

This would be for finding text like this in a larger string. 这将用于在较大的字符串中查找这样的文本。 If you want to validate a string instead, use 如果要验证字符串,请使用

^\p{L}{0,2}\d{4}$

这对我有用:

^[a-zA-Z]{0,2}\d{4}$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM