开发者

how can I get 2 alphabet 7 numeric 1 alphabet format for a string in regex in .net

example for th开发者_C百科e format is aa1231231c


Well that looks like:

[a-zA-Z]{2}[0-9]{7}[a-zA-Z]

to me. Of course it depends on what you mean by "alphabet" and "numeric" - the above only deals with ASCII letters and digits, with no accents, no other types of digits etc. You

Note that there are alternative approaches such as using \d for "any digit" and \p{L} to match "any letter"; if you really only want the ASCII letters and digits though, I'd use the above to make it obvious exactly what's allowed.

You can either match that as the whole string in code, or use ^ and $ to force it in the expression:

^[a-zA-Z]{2}[0-9]{7}[a-zA-Z]$

That will prevent your pattern being found in the middle of other text.


Are you validating an entire string?

Regex myPattern = new Regex(@"^[a-z]{2}\d{7}[a-z]$", RegexOptions.IgnoreCase);

No?

Regex myPattern = new Regex(@"[a-z]{2}\d{7}[a-z]", RegexOptions.IgnoreCase);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜