0

什么是 Java 中的正则表达式,用于在格式如下的字符串中捕获 @ 符号后的文本片段:

@+300 regex returns +300

@300 regex that should returns 300

@300.00 regex that should return 300.00

123123@300.00 should not return anything

@300.00@ should not return anything

@300.00.00 should not return anything

有点像 Twitter 中提到的用户名,但带有十进制数字(正数和负数)。

4

3 回答 3

1

试试这个:

String myString = "@123.45";
String returned = null;

Pattern p = Pattern.compile("^@([+-]?\\d+(?:\\.\\d+)?)$");
Matcher m = p.matcher(myString);

if(m.find()) {
    returned = m.group(1);
}
于 2013-07-27T12:55:31.803 回答
0

试试这个:

@([+-]?\d+(?:\.\d+)?)

然后考虑第一组出局。

于 2013-07-27T14:42:19.437 回答
-1

试试这个Java,让我知道它是否有帮助 -

    String toCheck = "@300.20";
    Pattern p = Pattern.compile("^@([+-]?\\d+(?:\\.\\d+)?)$");
    Matcher m = p.matcher(toCheck);

    if (m.matches()){
        System.out.println(m.group(1));
    } else {
        System.out.println("Oops! didn't match");
    }

对于 Javascript,这可能会有所帮助-

   var toCheck= "@300.20";
   var regex = /^@([+-]?\d+(?:\.\d+)?)$/;
   var matches = regex.exec(toCheck);
   var reqd = matches[1];
于 2013-07-27T13:16:23.113 回答