普通防爆pression以匹配特定的字符串 [英] Regular Expression to match specific string

查看:194
本文介绍了普通防爆pression以匹配特定的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正则表达式一直是我的致命弱点。我写的Web应用程序,在用户输入自己的标识符。我使用 RegexValidator 来验证此输入。

标识符应该是这样的:

TN-在-PL-KW-2012至1234年

这是该标识符是如何构建的:


  • 前两个字母是始终 TN

  • 后跟连字符

  • 然后两个字母,即要么是:的Et

  • 连字符

  • 双大写字母

  • 另一个连字符

  • 另外两个大写字母

  • 连字符

  • 四位数字,那是一年的时间,因此1970年和2012年之间的事情(我可以​​,只要有4位忽略)

  • 连字符

  • 序数,可以有从1到4位

请帮我写正则表达式来匹配这个标识符。


解决方案

  ^ TN-(在|特|呦|东部时间) -  [AZ] {2}  -  [AZ] { 2}  -  \\ d {4}  -  \\ d {1,4} $

正如一个评论,我建议你 Rubular 的,如果你想提高你的正则表达式的技能,这是一个简单实用的网页有记当你需要使用正则表达式的工作。

RegEx has always been my Achilles' heel. I am writing web app, where user will input his identifier. I'm using RegexValidator to validate this input.

The identifier should be something like this:

TN-In-PL-KW-2012-1234

And this is how the identifier is built:

  • first two letters are always TN
  • followed by hyphen
  • then two letters, that are either: In, Te, Yo or Et
  • hyphen
  • two uppercase letters
  • another hyphen
  • another two uppercase letters
  • hyphen
  • four digits, that are a year, so something between 1970 and 2012 (I can ignore that as long as there are 4 digits)
  • hyphen
  • ordinal number that can have from 1 to 4 digits

Please help me writing RegEx to match this identifier.

解决方案

^TN-(In|Te|Yo|Et)-[A-Z]{2}-[A-Z]{2}-\d{4}-\d{1,4}$

Just as a comment, I recommend you Rubular if you want to improve your regex skills, it's a simple and practical page to have in mind when you need to work with regex

这篇关于普通防爆pression以匹配特定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆