简体   繁体   English

匹配包含至少一个“ 5”数字的偶数

[英]Match even numbers containing at least one “5” digit

I have to find regular expression defining any even number, with at least one "5" digit inside. 我必须找到定义任何偶数且包含至少一个“ 5”数字的正则表达式。 I've been thinking about: 我一直在思考:

(0+1+2+3+4+5+6+7+8+9)* 5 (0+1+2+3+4+5+6+7+8+9)* (0+2+4+6+8)

Will this work? 这样行吗? Is there a way to make it shorter? 有没有办法使它更短?

Well, I'm not sure if this is the right website to post this question...but the tag exists :P 好吧,我不确定这是否是发布此问题的合适网站...但是标记存在:P

Your regexp looks good. 您的正则表达式看起来不错。 I don't think you can make it shorter (if we are speaking about theoretical regexps; real programming languages have shortcuts like [0-9] or \\d for any digit). 我认为您不能缩短它的时间(如果我们谈论的是理论上的正则表达式;实际的编程语言会使用[0-9]\\d这样的快捷键来表示任何数字)。

As pointed out by others your regexp will also match numbers starting from any number of zeroes. 正如其他人指出的,您的正则表达式还将匹配从零开始的数字。 If you don't want this, you will of course try to match the first digit with (1|2|3|4|5|6|7|8|9) , but now you've got a special case: what if the first digit is 5 ? 如果您不想这样做,您当然会尝试将第一个数字与(1|2|3|4|5|6|7|8|9)匹配,但是现在您有一个特殊情况:如果第一个数字是5 So you'll have to add more branches. 因此,您必须添加更多分支。

Your regexp is fine, but I think that making it match only numbers without leading zeroes is a good exercise and you should try it anyway, even if your task doesn't require this. 您的正则表达式很好,但是我认为让它只匹配不带前导零的数字是一个很好的练习,即使您的任务不需要这样做,也应该尝试一下。


Here is a general advice. 这是一般建议。 Problems requiring you to create regular expressions are best wil help of Test-driven development . 需要您创建正则表达式的问题对于测试驱动的开发是最好的帮助。 That is, before trying to write the regexp you create a set of tests for it. 也就是说, 尝试编写正则表达式之前 ,您需要为其创建一组测试。 It has a number of benefits: 它具有许多优点:

  1. If you've written many tests and you regexp passed them, you'll be almost sure you've got it write. 如果您编写了许多测试并且regexp通过了它们,则几乎可以肯定您已经编写了它。
  2. Quality of your tests will be way higher if you write them before creating regexps. 如果创建正则表达式之前编写测试,则测试的质量会更高。
  3. Seeing your freshly handcrafted regexp immediately pass your set of thorough tests makes you happy, trust me =). 看到您刚手工制作的正则表达式立即通过了一系列全面的测试,会让您感到高兴,请相信我=)。

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

相关问题 匹配包含至少一个字母和至少一位数字的单词 - match words containing at least one letter and at least one digit 正则表达式匹配包含至少一个数字和一个具有特定长度的数字的字符串 - Regex to match String containing at least one digit and one number with specific length 匹配每个 n 位的正则表达式至少有一个数字 1 - Regex to match each n digit has at least one digit 1 在第一个分隔符处拆分字符串,后跟包含至少一个数字的 substring - Split string at first delimiter followed by substring containing at least one digit 正则表达式只能匹配一个偶数小数位 - Regex to match only one even decimal digit 用 grep 匹配包含偶数的行 - Match lines containing even numbers with grep preg_match 匹配需要至少一个数字和至少一个字母的定义数量的字符 - preg_match match a defined number of characters requiring at least one digit and at least one letter java regex:用至少一个数字匹配一个又一个的字 - java regex: match word after another with at least one digit python regex匹配行,在字符串后包含数字,字符串末尾有数字 - python regex match line containing numbers after string with digit at end 如何匹配在Regex中包含至少一个字母和一个*的字符串 - how to match string that containing at least one alphabet and one * in Regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM