简体   繁体   English

Android-正则表达式将编辑文本格式化为xxx.xx

[英]Android - Regex to format the edittext as xxx.xx

I have taken some code from Limit Decimal Places in Android EditText . 我从Android EditText的Limit Decimal Places中提取了一些代码。

The Regex is as given below. 正则表达式如下。 I have used "3" as digitsBeforeZero and "2" as digitsAfterZero . 我已经使用“ 3”作为digitsBeforeZero和“ 2”作为digitsAfterZero

mPattern=Pattern.compile("[0-9]{0," + (digitsBeforeZero-1) + "}+((\\.[0-9]{0," + (digitsAfterZero-1) + "})?)||(\\.)?"); 

It works in all cases except xxx . 它适用于除xxx以外的所有情况。 When I try to enter the number " xxx.xx ", it does not allow me to enter the " dot (.) " after " xxx ". 当我尝试输入数字“ xxx.xx ”时,不允许我在“ xxx ”之后输入“ 点(。) ”。

Please help me to understand the Regex . 请帮助我了解正则Regex

Your expression can be decomposed into parts to be explained better. 您的表情可以分解为多个部分,以进行更好的解释。 Supposing you use 假设您使用

int digitsBeforeZero=3;
int digitsAfterZero=2; 

as you suggested, we have the expression: 如您所建议,我们具有以下表达式:

"[0-9]{0,2}+((\\.[0-9]{0,1})?)||(\\.)?"

the first part you have a symbol that can be any digit, the part inside brackets is a quantifier it tells how many of the previous symbol are allowed, this case it will accept 0, 1 or 2 digits, the plus symbol is also a quantifier that stands for "one or more" but as there is no symbol before it it is not needed and just obscures the expression. 第一部分中的符号可以是任意数字,方括号内的部分是一个量词,它告诉您允许使用多少个前一个符号,在这种情况下,它将接受0、1或2位数字,加号也是一个量词代表“一个或多个”,但是因为它前面没有符号,所以不需要它,只会使表达式模糊。 Inside parenthesis you will find a group, this is used to match and retrieve specific matches from your expression you can read more on groups here . 在括号内,您会找到一个组,该组用于匹配和检索表达式中的特定匹配项,您可以在此处阅读有关组的更多信息 The expression inside the nested parenthesis will accept a '.' 嵌套括号内的表达式将接受“。”。 character followed by 1 or 0 digits and the question mark outside of the parenthesis means that the expression previous to it can be or not be in the string to match. 字符后跟1或0位数字,并且圆括号外的问号表示它前面的表达式可以在字符串中,也可以不在字符串中。 Finally the '||' 最后是“ ||” is an logic 'or' meaning that it will also match the expression after it. 是逻辑“或”,表示它也将匹配其后的表达式。 That expression will accept a '.' 该表达式将接受“。”。 and it can be or not be present (the '?' quantifier) so it also matches an empty string. 并且它可以存在或不存在(“?”量词),因此它也匹配一个空字符串。

If what you want is only to match strings like xxxx.yyyy with n 'x' and m 'y' this is a better aproach: 如果您只想将xxxx.yyyy之类的字符串与n'x'和m'y'匹配,则这是一个更好的方法:

"[0-9]{0,"+n+"}(\\.[0-9]{0,"+m+"})?"

It is more clear, it will match an empty string as well, a single '.' 更清楚的是,它也将匹配一个空字符串,即单个“。”。 however it will also match strings like "333." 但是,它也将匹配“ 333”之类的字符串。 and ".33" so you have to tune it to your needs. 和“ .33”,因此您必须根据需要进行调整。

Remove -1 from both of your expressions.. 从两个表达式中删除-1

With above expression you are actually trying to match 0 to 2 digits before decimal and 0 to 1 digit after decimal for your input 3,2 and hence its not allowing you to enter decimal point( . ).. 使用上面的表达式,您实际上是在尝试为输入3,2匹配小数点前的0到2位数字和小数点后的0到1位数字,因此不允许您输入小数点( . )。

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

相关问题 android edittext textwatcher格式的电话号码,例如xxx-xxx-xx-xx - android edittext textwatcher format phone number like xxx-xxx-xx-xx 从 android 的编辑文本框中获取 XXX-XXX-XXXX 格式的电话号码 - getting phone number in XXX-XXX-XXXX format from edittext box in android 使用Cassandra Java驱动程序播放2.x-错误com.datastax.driver.core.Session-为xxx.xx创建池时出错,但返回响应 - Play 2.x with Cassandra Java Driver - ERROR com.datastax.driver.core.Session - Error creating pool to xxx.xx, but return responses 格式化数字值从 XX.X 到 .XXX java - Format number value from XX.X to .XXX java 正则表达式用于验证Android中的EditText - Regex for validating EditText in Android 如何在 Android 中创建电话号码格式 XXX-XXX-XXXX - How To Create Phone Number Format XXX-XXX-XXXX In Android 格式字符串变为xxx1,xx10或1 ###,10 ##等 - Format String become xxx1, xx10 or 1###, 10## etc 在Android中使用EditText将价格格式化为0.00格式 - Format Price in 0.00 format in android using edittext Android EditText资金输入正则表达式,不带“ $”符号 - Android EditText money input Regex without “$” symbol 如何在Android中使用自定义正则表达式验证EditText输入? - How to validate EditText input with a custom regex in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM