简体   繁体   English

正则表达式Java - 检查发票号码

[英]Regular Expression Java - check invoice number

I need to build a regular expression for checking the invoice number like this: FV/1024/02/2018我需要构建一个正则表达式来检查发票号码,如下所示: FV/1024/02/2018

  • FV - using always FV nothing different FV - 始终使用 FV 没什么不同
  • 1024 - next numer of invoice - alway starting with "1" next three numbers are different [0-9] 1024 - 发票的下一个数字 - 总是以“1”开头,接下来的三个数字不同[0-9]
  • 02 - month - always on number 1-12 02 - 月 - 总是在 1-12 号
  • 2018 - a year 2018 - 一年

i have:我有:

Pattern pattern = Pattern.compile("([FV])/((1)[0-9]{3})/([0,1]{1}[0-9]{1})/([0-9]{4})");

and i know..."something" wrong ...我知道……“有事”错了……

This should work这应该工作

FV\/1\d{3}\/(:?(:?0[1-9])|1[0-2])\/\d{4}

Demo演示

  • FV\\/ matches first, constant part ( FV/ ) string FV\\/首先匹配常量部分 ( FV/ ) 字符串
  • 1\\d{3}\\/ is an invoice number - starting with 1 and followed by 3 digits 1\\d{3}\\/是发票编号 - 以 1 开头,后跟 3 位数字
  • (:?(:?0[1-9])|1[0-2]) is a month number (:?(:?0[1-9])|1[0-2])是月份数
    • (:?0[1-9]) from 01 to 09 OR (:?0[1-9])0109
    • 1[1-2] from 10 to 12 1[1-2]1012
  • \\/\\d{4} is slash year, eg /2018 \\/\\d{4}是斜线年份,例如/2018

something like this?像这样的东西?

FV\/\d+\/\d+\/\d+

Preview 1预览 1

or或者

FV\/(1[0-9]{3})\/([0,1]{1}[0-9]{1})\/([0-9]{4})

Preview 2预览 2

This should work (it matches your given invoice number: 'FV/1024/02/2018'):这应该有效(它与您给定的发票编号匹配:“FV/1024/02/2018”):

FV\/(1[0-9]{3})\/([0-9]{2})\/([0-9]{4})

The Invoice Number, Month an Year each have their own Group.发票编号,每年每月都有自己的组。


Demo Regex101演示 Regex101

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

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