用于发票格式的 RegEx [英] RegEx for an invoice format
问题描述
我对正则表达式很陌生,我正在尝试创建一个用于验证发票格式的正则表达式.
I'm quite new to regular expressions and I'm trying to create a regex for the validation of an invoice format.
模式应该是:JjYy(所有4个字符都是合法的),使用了0、2或4次例如根本没有 Y 有效,YY 有效,YYYY 有效,但 YYY 应该失败.随后是一系列重复 3 到 10 次的 0.全文不得超过 10 个字符.
The pattern should be: JjYy (all 4 characters are legit), used 0, 2 or 4 times e.g. no Y's at all is valid, YY is valid, YYYY is valid, but YYY should fail. Followed by a series of 0's repeating 3 to 10 times. The whole should never exceed 10 characters.
示例:JyjY000000 有效(虽然很奇怪)YY000 有效000000 有效jjj000 无效jjjj0 无效
examples: JyjY000000 is valid (albeit quite strange) YY000 is valid 000000 is valid jjj000 is invalid jjjj0 is invalid
我从 此处 学到了一些基础知识,但是我的正则表达式在不应该失败时失败了.有人可以协助改进吗?
I learned some basics from here, but my regex fails when it shouldn't. Can someone assist in improving it?
到目前为止我的正则表达式是:[JjYy]{0}|[JjYy]{2}|[JjYy]{4}[0]{3,10}
.
My regex so far is: [JjYy]{0}|[JjYy]{2}|[JjYy]{4}[0]{3,10}
.
以下也失败了:[JjYy]{0|2|4}[0]{3,10}
推荐答案
由于你需要总长度不超过 10 个字符我认为你必须分别处理三种前缀:
As you need the total length to never exceed 10 characters I think you have to handle the three kinds of prefixes separately:
0{3,10}|[JjYy]{2}0{3,8}|[JjYy]{4}0{3,6}
这篇关于用于发票格式的 RegEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!