English 中文(简体)
定期表达方式按照要求不正确
原标题:Regular Expression not work correctly according to the requirement

我对我经常表达方式感到困惑。 我以前想要的是,用户不希望进入的最大数字是3,数字不应从零开始。 数位数在位数之前,即三位数或两位数或一位数,但不能从零开始。 我想让用户在奇马尔点之后进入一位数,这不应超过一位数。 因此,这些模式是什么?

例:

<Number允许进入:1.3,22.3,333.4,999.6

<>0> 不允许进入:0,0.1,.1,444444.67,333.78

www.un.org/spanish/ecosoc 《刑法》第一版

tempEditText.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(3,1)});
class DecimalDigitsInputFilter implements InputFilter
{
    Pattern mPattern;

    public DecimalDigitsInputFilter(int digitsBeforeZero, int digitsAfterZero)
    {
        mPattern = Pattern.compile("[0-9]{0," + (digitsBeforeZero - 1) + "}+((\.[0-9]{0," + (digitsAfterZero - 1) + "})?)");
    }
    
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
    {
        Matcher matcher = mPattern.matcher(dest);
        if(!matcher.matches())
        {
            return "";
        }
        return null;
    }
}
最佳回答

我认为,你想要的模式是:

^[1-9]d{0,2}.d?$

解释:

^        // Start of string
[1-9]    // First digit can t be 0
d{0,2}  // Zero, one, or two more digits after first digit
.       // Literal decimal point
d?      // Optional single digit after decimal point
$        // End of string

在你的法典中:

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

你的亲子点和挂号位数表示,像“1”这样的数字不匹配。 这里的版本要求删除点,随后的数位数可选择:

mPattern = Pattern.compile("^[1-9]\d{0," + (digitsBeforeZero - 1)
           + "}\.\d{0," + digitsAfterZero + "}?$");
问题回答
Pattern.compile("[1-9]{0," + (digitsBeforeZero - 1) + "}+((\.[0-9]{0," + (digitsAfterZero - 1) + "})?)"

......

你有以下问题:

"[0-9]{0," + (digitsBeforeZero - 1) + "}+((\.[0-9]{0," + (digitsAfterZero - 1) + "})?)"
 ^^^^^                                              ^
  1                                                 2
  1. 你的模式从一开始就允许0,但你不希望领导0。

  2. 你允许在 do子之后有0位数,但你不想。

  3. 你们需要紧靠你们的reg,以避免部分相匹配(允许允许“Foo123.4Bar”)

因此,

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签