用正则表达式查找某些数字 [英] Finding certain numbers with regex

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

问题描述

我需要查找给定数字是否在0到500之间,如果是,则替换一个单词。此外,如果一个数字介于501和900之间,它应该用不同的单词代替。



我尝试过:



不多,我正在玩正则表达式测试仪,我找不到解决方案。

I need to find if a given number is between 0 and 500 and if so, replace a word. Furthermore, if a number is between 501 and 900 it should replace by a different word.

What I have tried:

Not much, I'm playing with regex tester and i can't find a solution.

推荐答案

当你可以做到这一点,它不容易,不易阅读,而且肯定无法维护!



正则表达式是文本处理系统,它们没有概念=大于或小于 - 只是基于文本的匹配。

而不是这样做,使用正则表达式提取数字,并使用索引和长度信息,它将为您提供替换用你的演讲语言中基于数字值的单词编号。



尝试用正则表达式做任何事就像用锤子拧螺丝:是的它可以工作,但很快就会希望它没有!
While you can do this, it's not easy, not readable, and most certainly not maintainable!

Regular expresses are text processing systems, they don't have a concept =of "greater than" or "less than" - just of text based matches.
Instead of doing this, use a regex to extract the number, and use the index and length info it will give you to replace the number with words based on it's numeric value in your presentation language.

Trying to do everything with a regex is like using a hammer to fit screws: yes it can work, but pretty quickly you will wish it hadn't!


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

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