简体   繁体   English

正则表达式Android数字

[英]Regular Expression Android For Number

I've been here in a battle because of regular expressions .. 因为正则表达式,我一直在战斗中。

I'm getting something like that in Response [14-23.0, 14-23, 23.320] .I will Explain clearly in steps. 我在回复[14-23.0, 14-23, 23.320]得到了类似的内容。我将在步骤中清楚地解释。

a) The value which comes in response should be set to EditText.(Which i have Done) a)响应中的值应设置为EditText。(我已完成)
b) The Value Which Comes in response Should not be Validated. b)不应验证应对的价值。
c) The Values which Comes other than response should be validated. c)应验证除响应之外的其他值。

Below are the string which need not be validated. 以下是无需验证的字符串。

a)14-23.0 A)14-23.0
b)14-23 B)14-23
c)23.320 C)23.320

The Scenario Which need to be validated. 需要验证的场景。

a)14--23.0 一)14--23.0
b)1.4-23.0 B)1.4-23.0
C)14-2.3.0 C)14-2.3.0

Following Code i uses Which Doesn,t works. 以下代码我使用了哪些无效,无效。

public static String BondPricePatternValidation(String mString){

        String pattern = "(\\d+)-(\\d+).(\\d+)";

        // Create a Pattern object
        Pattern r = Pattern.compile(pattern);

        // Now create matcher object.
        Matcher m = r.matcher(mString);
        if (m.find( )) {
            System.out.println("Found value: " + m.group(0) );
            System.out.println("Found value: " + m.group(1) );
            System.out.println("Found value: " + m.group(2) );
        } else {
            System.out.println("NO MATCH");
        }
        return pattern;

    }

Someone can help me in the regular expression to do this? 有人可以帮我正则表达式吗?

^(\\\\d+-)?(\\\\d+)\\\\.?\\\\d+$ should do the trick. ^(\\\\d+-)?(\\\\d+)\\\\.?\\\\d+$应该可以解决问题。

Let me know if it suits your needs. 如果它符合您的需求,请告诉我。

I'm not sure I understand your question. 我不确定我理解你的问题。 So let me re-state your question as I understand it. 所以,让我按照我的理解重新陈述你的问题。 You're looking for a regular expression that only allows either (1) a float-like number alone, or (2) what looks like a range between an integer-like number and either another integer or even a float-like number, strictly with one dash in between. 你正在寻找一个正则表达式,它只允许(1)一个类似浮点数的数字,或者(2)看起来像一个类似整数的数字和另一个整数之间的范围 ,或者甚至是类似浮点数的数字中间有一个短划线。 Probably this isn't exactly it, but let me answer the question this way, and as you clarify I'll edit. 可能这不完全是这样,但让我以这种方式回答这个问题,当你澄清我会编辑。

Let's start with the second alternative, the rangE . 让我们从第二个选择,即rangE开始 You probably don't want numbers like 0004 , so here's how you match a "regular looking" integer: 你可能不想要像0004这样的数字,所以这里是你如何匹配“常规”整数:

[1-9]\\d*

Then, you want exactly one dash, easy enough: 然后,你想要一个简单的,很简单:

[1-9]\\d*-

Then, another number, except this one can be float-like, so let's use the quantifier ? 那么,除了这个之外的另一个数字可以是浮点数,那么让我们使用量词? :

[1-9]\\d*-[1-9]\\d*(?:\\.\\d+)?

Now, to complete this, we just have to make the whole range part optional as well: 现在,为了完成这个,我们只需要使整个范围部分也是可选的:

[1-9]\\d*(?:-[1-9]\\d*(?:\\.\\d+)?)?

And that should be it. 这应该是它。 By the way, as you can see, you need to escape your . 顺便说一句,正如你所看到的,你需要逃避你的. s, as otherwise they stand for wildcards. s,否则他们代表通配符。

One last bit is concerning anchoring. 最后一点是关于锚定。 You might simply add ^ and $ on each end, so as not to permit other characters on each side: 您可以简单地在每一端添加^$ ,以便不允许每一侧的其他字符:

^[1-9]\\d*(?:-[1-9]\\d*(?:\\.\\d+)?)?$

However if you intend to match this pattern within a larger block of text, you need to use a negative lookbehind assertion: 但是,如果您打算在更大的文本块中匹配此模式,则需要使用负向lookbehind断言:

(?<![1-9])[1-9]\\d*(?:-[1-9]\\d*(?:\\.\\d+)?)?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM