简体   繁体   English

正则表达式十六进制字符限制

[英]Regex hex character limit

I need to make a (C#) regular expression that matches codes with a few rules:我需要制作一个(C#)正则表达式来匹配具有一些规则的代码:

  1. The accepted characters are hex (and capitalized), (therefore 0-9 AF): ^[A-F0-9]*$接受的字符是十六进制(和大写),(因此是 0-9 AF): ^[A-F0-9]*$
  2. The number of characters is between 9 and 10: ^[A-F0-9]{9,10}$字符数在 9 到 10 之间: ^[A-F0-9]{9,10}$
  3. The same character cannot appear more than twice consecutively (eg. AAABCDEF0 not allowed) ?同一个字符不能连续出现两次以上(例如,不允许AAABCDEF0
  4. No more than 5 letters in the code (eg. A1BC2E3FF4 not allowed) ?代码中不超过 5 个字母(例如,不允许使用A1BC2E3FF4
  5. No more than two numbers are equal (eg. A1BC1E1234 not allowed) ?不超过两个数字相等(例如,不允许使用A1BC1E1234
  6. No more than three letters are equal (eg. A1AA1234A5 not allowed) ?不超过三个字母相等(例如,不允许使用A1AA1234A5

Keep in mind, that the rules 3-6 can occur anywhere within the sentence, not necessarily the start).请记住,规则 3-6 可以出现在句子中的任何位置,不一定是开头)。 If anyone of you can help me with any of the rules between 3 and 6, it will be much appreciated :)如果你们中的任何人可以帮助我解决 3 到 6 之间的任何规则,我将不胜感激:)

Thanks!谢谢!

Your question does seem to come out of nowhere (homework?)... What's the problem you're trying to solve?您的问题似乎无处不在(家庭作业?)...您要解决的问题是什么?

If you want to do it yourself, in order to get you started you should try learning about positive and negative lookahead .如果你想自己做,为了让你开始,你应该尝试学习积极和消极的前瞻

They're usually a great and easy way to add condition on a string.它们通常是在字符串上添加条件的好方法。

For example, this regex will ( (?:...) is a non capturing group):例如,这个正则表达式将( (?:...)是一个非捕获组):

#^(?=.{9,10}$)(?!(?:.*[A-Z]){6})[A-F0-9]*$

^
(?=.{9,10}$)            # check the regex is 9 or 10 charac long
(?!(?:.*[A-Z]){6})      # check there are no more than 5 letters
[A-F0-9]*$              # actually match the string (not mandatory if you just need to validate)

If you want to test it online, regex101 is usually a great website to learn and goof around.如果你想在线测试它,regex101 通常是一个很好的学习和闲逛的网站。

Good luck !祝你好运 !

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

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