普通的前pression匹配的美元金额 [英] Regular expression to match dollar amounts

查看:123
本文介绍了普通的前pression匹配的美元金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁以下模式提供我经常EX pression?

$ 1234

$ 31234.3

$ 1234.56

$ 123456.78

$ 99

我的要求是十进制数字前应不超过6小数点后不应超过2位数。请帮帮我。在此先感谢..


解决方案

  ^ \\ $(?=。* \\ D)\\ D {0,6}(\\。\\ D { 1,2})$?

(?=。* \\ D)确保有字符串中的至少一个数字。如果没有,正则表达式 ^ \\ $ \\ D {0,6}(\\。\\ D {1,2})?$ 将匹配字符串 $

顺便说一句,超前不一定是这个长;即使是一个简单的(?=)会做,因为正则表达式可以确保后续字符确实有效。因此,它可以被简化为

  ^ \\ $(?=)\\ D {0,6}(\\。\\ D {1,2})$?

Could anybody provide me the regular expression for the following patterns?

$1234

$31234.3

$1234.56

$123456.78

$.99

My requirement is the digits before decimal should not exceed 6 and after the decimal point it should not exceed 2 digits. Please help me. Thanks in advance..

解决方案

^\$(?=.*\d)\d{0,6}(\.\d{1,2})?$

(?=.*\d) makes sure that there is at least one digit in the string. Without that, the regex ^\$\d{0,6}(\.\d{1,2})?$ would match the string $.

Btw, the lookahead need not be this long; even a simple (?=.) would do, as the regex makes sure that the subsequent characters are indeed valid. Thus, it can be simplified to

^\$(?=.)\d{0,6}(\.\d{1,2})?$

这篇关于普通的前pression匹配的美元金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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