正则表达式排除某些数字 [英] regex to exclude certain number

查看:189
本文介绍了正则表达式排除某些数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

^(230 | 231)\d {6} $ | ^(01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38)\d {7 } $



在上面的正则表达式中我想排除18和19我该怎么办

解决方案

< BLOCKQUOTE> | ^(01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38)\d {7}





在上面的正则表达式中我想排除18和19我怎么能这样做


简单:不要使用正则表达式。

正则表达式用于字符串中的模式匹配 - 您已经可以看到您的正则表达式变得令人讨厌且难以使用。在正则表达式中进行字符串匹配,然后使用代码转换匹配的数字并在那里检查它们。



正则表达式是很棒的工具 - 但只有当它们用于相应的任务时才会使用。



如果必须的话使用正则表达式,然后取出18 | 19 |匹配字符串的一部分。但说真的,我会在这里使用代码。


^(230|231)\d{6}$|^(01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38)\d{7}$

In the above regex i want to exclude the 18 and 19 how can i do so

解决方案

|^(01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38)\d{7}




In the above regex i want to exclude the 18 and 19 how can i do so


Simple: don''t use a regex.
Regexs are for pattern matching in strings - you can already see that your regex is getting nasty and difficult to work with. Do string matching in the regex, then use code to convert matched numbers and check them there.

Regexes are wonderful tools - but only when they are used on appropriate tasks.

If you must use a regex, then just take out the "18|19|" part of the match string. But seriously, I''d use code here.


这篇关于正则表达式排除某些数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆