简体   繁体   English

号码内号码的正则表达式模式

[英]Regex pattern for a number within a number

Can anyone think of a better way to write this? 谁能想到一种更好的方式编写此内容? It works but it is a little ugly. 它可以工作,但是有点难看。

Input data looks like this: 125100001 输入数据如下:125100001

The first two numbers are the year, next two are the week number, and last 5 are the serial. 前两个数字是年份,后两个数字是星期数,后五个数字是序列号。 I want to validate that the week number is not over 52 for an angular input[number] pattern option. 我想验证角度输入[数字]模式选项的星期数是否不超过52。 Basically just to leverage the $error field :) 基本上只是利用$ error字段:)

So here it is: 所以这里是:

^\d\d(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-2]){1}\d{5}$

Use this: 用这个:

^(\d{2})([0-4][1-9]|[1-5]0|5[12])(\d{5})$

Notes 笔记

  • The first set of parentheses (0[1-9]|1[0-2]) validates the month: 01-12 第一组括号(0[1-9]|1[0-2])验证月份:01-12
  • The second set of parentheses ([0-4][1-9]|[1-5]0|5[12]) validates the week: 01-52 第二组括号([0-4][1-9]|[1-5]0|5[12])验证星期:01-52
  • If you wish, you can retrieve each component with groups 1, 2 and 2 如果需要,可以检索具有组1、2和2的每个组件

Just for the week part: 仅针对一周部分:

[0-4]\d|5[0-2]

so the entire regex would be: 因此整个正则表达式将是:

^\d\d([0-4]\d|5[0-2])\d{5}$

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

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