在字符串中查找十进制

时间:2014-01-21 06:08:50

标签: java regex

我有一个这样的字符串:

  

感谢您支付保险费586.48卢比。您的   此付款的交易编号是981562359815.您的卡将是   借记于2020-07-15。

我需要使用正则表达式单独提取事务编号的小数。小数位数可能会不时变化。

Pattern.compile("(?i)(transaction number *?)(.+?)(\\.)")

使用上面的模式,我试图提取,但我不能用这种方法成功。有没有有效的方法?

2 个答案:

答案 0 :(得分:1)

假设字符串.与您要搜索的号码之间可能没有点(transaction number),请使用

Pattern regex = Pattern.compile("(?i)transaction number [^.]*\\b(\\d+)\\.");
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
    ResultString = regexMatcher.group(1);
} 

<强>说明:

(?i)    # case insensitive matching mode
transaction\ number  # Match this literal text
[^.]*   # Match any number of characters except dots
\b      # Match the position at the start of a number
(\d+)   # Match a number (1 digit or more), capture the result in group 1
\.      # Match a dot

如果您只是想在transaction number之后找到第一个数字,请使用

Pattern.compile("(?i)transaction number\\D*(\\d+)")

\D匹配任何不是数字的字符。

答案 1 :(得分:1)

试试这个

 s = s.replaceAll(".* is (\\d+).*", "$1");
相关问题