2014-01-21 33 views
0

我有一個字符串,像這樣:查找十進制字符串中的

感謝您支付的盧比5896.48您的保險單。您的 此次付款的交易號碼爲981562359815.您的卡將於2017-07-15扣除 。

我需要使用正則表達式提取單獨的事務數的小數。小數點數可能隨時間變化。

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

使用上面的模式我嘗試提取,但我無法用這種方法成功。有沒有有效的方法?

+0

如果你知道交易編號前面帶有文字:「此付款您的交易編號爲」你可以用'的indexOf()'。如果不是,你能否假設所有的交易號碼都是12號? – alfasin

+0

有趣的是,沒有半點想法的人如何堅持*高效*的做法。 – Ingo

回答

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匹配任何不是數字的字符。

+0

但我需要單獨的小數...... – Vinesh

+0

然後你只需要在比賽結束後訪問組1的內容。編輯我的答案... –

+0

假設如果我在'981562359815'末尾沒有點,是否有任何事情要得到第一個十進制序列而不是最後一個。 – Vinesh

1

試試這個

s = s.replaceAll(".* is (\\d+).*", "$1"); 
相關問題