DEV Community

Parsing Bank SMS From India on Android: 9 Edge Cases From Real Messages (Kotlin)

A parser that looks for credit and debit breaks on the first card SMS it meets: "Rs.499 spent on HDFC Bank Credit Card" contains the word "Credit" and is a spend.

Most of what I learned writing a parser for bank SMS in India looks like that: the formats are easy to match, and the words are what mislead you.

I'm building DailyBudget, an Android budget app that reads bank SMS on the phone and turns them into one number: what you can spend today. It has no INTERNET permission, so all of this runs on the device. It reads HDFC Bank, SBI, ICICI Bank, Axis Bank, Kotak, PNB, Bank of Baroda and Canara Bank by SMS, plus FamPay through app notifications.

Short version: read new SMS with a BroadcastReceiver on SMS_RECEIVED and old ones with a ContentResolver query on the inbox, filter by the bank's DLT sender ID, then parse with one small regex spec per bank (amount, direction, merchant, reference). Then spend most of your time on the 9 cases below, each one a unit test built from a real SMS.

The parser has 235 unit tests across those 8 banks (36 HDFC, 31 SBI, 41 ICICI, 22 Axis, 22 Kotak, 29 PNB, 30 Bank of Baroda, 24 Canara), plus separate tests for the money rules. Each edge case below has at least one of those tests behind it.

The pipeline in four steps

Step What runs Notes
1. Receive SmsReceiver on SMS_RECEIVED_ACTION Long SMS arrive in parts; join every part's body before parsing
2. Filter isBankSender(sender) DLT sender IDs look like AX-HDFCBK or VM-HDFCBK-S ; IDs ending in -P are promotional and skipped
3. Parse TransactionParser.parse(body, sender) The sender picks the bank's spec; returns amount in paise, debit or credit, merchant, reference
4. Store Repository.ingestSms Dedupe, category, refunds and reversals, then the daily number updates

For the first run, the same parser goes over the inbox with a ContentResolver query on Telephony.Sms.Inbox.CONTENT_URI, filtered by date and sorted oldest first. Both paths call the same ingestSms, behind one mutex, so the receiver and the backfill can't race each other into a duplicate.

Amounts are stored as Long paise. "1,250.5" goes through BigDecimal, moves the point right by two and rounds half-up to 125050 (code in edge case 6).

9 edge cases from real bank SMS in India

1. "Credit Card" contains the word credit

As in the intro, plain credit and debit can't decide the direction, so they're left out of the direction list on purpose.

The parser looks for verbs: spent, debited, paid, withdrawn, purchase for money out; received, credited, refunded, reversed, deposited for money in.

2. Both directions in one message

A UPI debit often says "debited from A/c XX1234 ... credited to VPA shop@okaxis". Both verbs are there. The rule: whichever direction word appears first wins. The account holder's side usually comes first.

That rule fails for outgoing NEFT, where SBI and others write "credited to the beneficiary". Those banks' specs carry a forceDebit pattern for exactly that phrase. Kotak has the reverse: "Cashback ... sent to your A/c" is money in, so Kotak's spec has a forceCredit for "sent to your".

In the parser it's one when, in this order:

// d, c = index of the first debit / credit word in the SMS, or null
val isDebit = when {
  bank.forceDebit?.containsMatchIn(text) == true -> true // "credited to the beneficiary"
  bank.forceCredit?.containsMatchIn(text) == true -> false // Kotak "sent to your A/c"
  reversal -> false // "amount reversed": money came back
  d == null && c == null -> return null // no direction word: don't guess
  d == null -> false
  c == null -> true
  else -> d < c // first word wins
}

3. The same debit, sent twice

HDFC sends some debits twice, seconds apart: one "...towards MERCHANT" with no reference, then one Info: UPI-<ref>-MERCHANT with a reference. Counting both doubles the spend.

The rule in the repository:

  • If either SMS has no reference: same amount and same direction within 2 minutes = one transaction.
  • If both have references: always two transactions.

I needed the second rule as much as the first. Two โ‚น10 chais paid 30 seconds apart are two real payments with two references. A dedupe on amount alone would drop one without telling anyone. There's a test for exactly that case.

4. Failed is not the same as reversed

"UPI txn failed" means no money moved: drop it. "Amount reversed to your account" means money came back: store it as a credit and cancel the earlier debit.

The failure regex only applies when there's no reversal word in the message, otherwise the refund would be thrown away with the failure.

5. SMS that mention money but moved none

A lot of bank SMS contain "Rs" and an account number and are not transactions. Every one of these is ignored before parsing:

  • OTP - No money moved yet
  • "will be debited" / "will be auto-debited" - Pre-debit notice for an autopay; the real debit SMS comes later
  • Mandate set up, collect request, IPO "blocking of funds" - Money is reserved, not spent
  • Card bill "is due", "total amount due" - Stored as a card bill with its due date, never a spend
  • Balance-only alert, e-statement, "pre-approved" offers - Information or marketing

The card-due SMS is the interesting one. If it were read as a spend, every card purchase would be counted twice: once when you swipe, once when the bill arrives.

6. No currency marker at all

One SBI UPI format reads "A/C X1001 debited by 305.0 on date 08Oct25 trf to ...". There's no Rs, INR or โ‚น before the amount, so the shared amount regex finds nothing. SBI's spec adds its own amount pattern for debited by <number>, tried before the shared one.

Whatever pattern matches, the amount goes through one function and is stored as whole paise in a Long. No Double anywhere, so "305.0", "1,20,000.00" and "โ‚น99" all come out exact:

fun toPaise(rupees: String): Long? = rupees.replace(",", "").trim().toBigDecimalOrNull()?.movePointRight(2)?.setScale(0, RoundingMode.HALF_UP)?.toLong()

Every format gets a test built from a real SMS. This one is SBI's no-marker UPI debit:

@Test fun upiDebitWithoutCurrency() {
  val t = TransactionParser.parse("Dear UPI user A/C X1001 debited by 305.0 on date 08Oct25 trf to AAHARA CAFE " + "Refno 100000000001 If not u? call-18001234567 for other services-9223766666-SBI", sender = "VM-SBIUPI-S",)!!
  assertEquals(30500L, t.amountPaise)
  assertTrue(t.isDebit)
  assertEquals("AAHARA CAFE", t.merchant)
  assertEquals("100000000001", t.ref)
}

7. Foreign card spends

"USD 11.80 spent using ... Card" and "Spent USD 100 ..." both end with "Avl Limit: INR 17,95,899.53". The only INR figure in the message is the available limit. A naive parser records a โ‚น17.9 lakh spend.

DailyBudget skips any message that starts with a non-INR currency code. It doesn't convert the dollar amount, because the rupee figure the bank actually charges arrives later on the statement, not in this SMS. Skipping is better than guessing.

8. Letters that aren't ASCII

Some SBI Card SMS write words in Unicode mathematical sans-serif letters. They look normal on screen but don't match [A-Za-z]. The fix is one line: run the body through Normalizer.normalize(body, Normalizer.Form.NFKC) before any regex. NFKC folds those characters back to plain ASCII.

9. Merchant names full of template junk

The payee comes out as things like "UPI-SBIFUNDS1234567", "_JP PETROLEUM." or "name@okaxis". The normalize step trims leading symbols, drops UPI-, IMPS-, NEFT-, POS- prefixes, cuts the VPA after @, removes "PVT LTD" style suffixes and trailing digit runs, then uppercases and caps the length at 40.

A merchant also needs at least three letters in a row, which rejects phone numbers, times and masked VPAs that patterns sometimes catch.

Don't drop what you can't read

Some SMS will slip through. If a message from a bank sender has an amount and an account, card or UPI mention but no direction word the parser knows, DailyBudget stores it as "couldn't read" and shows it to the user.

The parser also has a version constant. When it goes up, the app re-checks every stored "couldn't read" SMS on the next start, so a parser fix also reaches messages that arrived before it.

One spec per bank

HDFC keeps its original rules. Every other bank is one BankSpec: sender ID regex, extra amount patterns, its own direction words, ignore list, merchant patterns, reference patterns, footers to strip, and optional forceDebit, forceCredit and ATM patterns. A shared parseWith runs any spec.

Adding a bank is: collect real sample SMS, write a failing test per sample, add one spec. The PNB, Bank of Baroda and Canara specs went in that way. A few banks we don't parse yet are only counted by sender ID, never read, so we know which one to add next.

Permissions, and the Play Store question

The manifest has RECEIVE_SMS and READ_SMS, and no INTERNET. That's the privacy argument in one line: the parsed data has no network path out.

One thing to know before you build this: on Google Play, the SMS permissions are restricted. Only apps whose core function needs them can use them, and you have to declare why. DailyBudget is in early access and installed directly for now, so it hasn't been through that review yet. Plan for it early if you're building something similar.

If you want the model side of the app, the offline LLM write-up covers how the advisor runs on the phone and why it never writes numbers. For the user side, expense tracker without bank login compares SMS reading with the other ways to track spending, and how it works shows the app.

Code details are from the DailyBudget Android source as of v2.5.1 (1 Dec 2026). Bank SMS formats change; test against your own samples.

Since v2.5.0 the app doesn't wait for you to open the "couldn't read" list: an unreadable bank SMS turns into one lock-screen question, "โ‚น199 ยท is this a spend?", with Count it, Category and Not a spend.

I built this. The original, with sources, is on the DailyBudget site: Parsing bank SMS from India on Android: 9 edge cases.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.