我想從短信中提取余額
我的短信內容是
account ending with ********9415 has been credited with Rs. 5000. Updated account balance is Rs. 13086.18
Your card transaction of Rs.417 is successful. Your updated credit balance is Rs.78,468
Dear Cardmember, payment of Rs.7657.00 has been received towards your Bank Credit Card ending with 3459 on 12-11-2020 through NEFT. Payment is subject to realisation. Your available Credit limit now is Rs. 173281.31.
到目前為止,這是我的代碼
(?i)(?:\sbalance\s*)([A-Za-z0-9] \s[A-Za-z0-9] )
(?i)(?:\scredit limit\s*)([A-Za-z0-9] \s[A-Za-z0-9] )
那么如何從上面的短信中獲取金額呢?
uj5u.com熱心網友回復:
您可以使用
(?i)\b(?:balance|credit\s limit)\D (\d (?:[.,]\d )?)
請參閱正則運算式演示。詳情:
(?i)- 不區分大小寫的嵌入標志選項\b- 一個詞邊界(?:balance|credit\s limit)-balance或之間credit limit有任何一個或多個空格\D- 一個或多個非數字字符(\d (?:[.,]\d )?)- 第 1 組(您需要獲取的值):一位或多位數字,然后是可選的.or,和一位或多位數字。如果可以有多個點/逗號,請替換?為*。
請參閱Java 演示:
String regex = "(?i)\\b(?:balance|credit\\s limit)\\D (\\d (?:[.,]\\d )?)";
String text = "account ending with ********9415 has been credited with Rs. 5000. Updated account balance is Rs. 13086.18\n\nYour card transaction of Rs.417 is successful. Your updated credit balance is Rs.78,468\n\nDear Cardmember, payment of Rs.7657.00 has been received towards your Bank Credit Card ending with 3459 on 12-11-2020 through NEFT. Payment is subject to realisation. Your available Credit limit now is Rs. 173281.31.";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find()){
System.out.println(matcher.group(1));
}
輸出:
13086.18
78,468
173281.31
uj5u.com熱心網友回復:
如果您的資料始終具有該格式,您可以使用捕獲組并匹配 Rs。來自示例資料:后跟字符 a-zA-Z 和空格:
(?i)\b(?:balance|credit\s limit)\s [A-Za-z] (?:\s [A-Za-z] )*\s Rs\.\s*(\d (?:[.,]\d )*)
(?i)不區分大小寫匹配的行內修飾符\b(?:balance|credit\s limit)匹配 2 個選項之一\s [A-Za-z]匹配 1 個空格字符和 1 個字符 A-Za-z(?:\s [A-Za-z] )*可選地重復前面的模式,前面有 1 個空白字符\s Rs\.\s*匹配 1 個空白字符和Rs.(\d (?:[.,]\d )*)捕獲第 1組中的1 位數字,可選擇重復.或,和 1 位數字
查看正則運算式演示| Java 演示。
String regex = "(?i)\\b(?:balance|credit\\s limit)\\s [A-Za-z] (?:\\s [A-Za-z] )*\\s Rs\\.\\s*(\\d (?:[.,]\\d )*)";
String string = "account ending with ********9415 has been credited with Rs. 5000. Updated account balance is Rs. 13086.18\n\n"
"Your card transaction of Rs.417 is successful. Your updated credit balance is Rs.78,468\n\n"
"Dear Cardmember, payment of Rs.7657.00 has been received towards your Bank Credit Card ending with 3459 on 12-11-2020 through NEFT. Payment is subject to realisation. Your available Credit limit now is Rs. 173281.31.";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
輸出
13086.18
78,468
173281.31
uj5u.com熱心網友回復:
最好避免在此處撰寫復雜的正則運算式。它很容易出錯,而且這種錯誤很難除錯。
我們可以做的是通過一系列字串拆分來簡化問題并將其轉化為一系列較小的子問題。
- 首先替換“Rs”。帶有“XXX”字樣的子串。我們需要這樣做來洗掉點,
Rs.以便以后.可以用作拆分標記。 - 在“.”上拆分字串。現在符號并將其保存到陣列 'sentences'
- 對于
sentences陣列中的每個字串,僅選擇“XXX”部分之后的所有內容。這將為您提供價格字串“32.12” - 現在您只需使用
Double.valueOf()
維護和理解這樣的代碼會容易得多,而不是使用正則運算式的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351515.html
下一篇:字符類中的RegExp重復范圍
