If you want to experience the full spectrum of linguistic inconsistency, try writing a parser for Indian banking SMS notifications.
India possesses one of the most advanced real-time digital payment ecosystems in the world, processing billions of UPI and card transactions every month. Yet the text alerts sent by Indian banks follow no unified industry standard:
- State Bank of India (SBI) writes: "Dear UPI user A/C 1234 debited by 500.0 on 24Oct26 by transfer to VPA..."
- HDFC Bank writes: "Sent Rs. 1,450.00 from HDFC Bank A/C **4921 to ZOMATO on 24-OCT-26 via UPI Ref 429810..."
- ICICI Bank writes: "ICICI Bank Acct XX8012 debited with INR 2,999.00 on 24-Oct-26. Info: BIL*NETFLIX..."
- Axis Bank writes: "Spent Rs 450.00 on Axis Bank Card no. XX4321 at SHELL PETROL..."
Currency symbols range from Rs., Rs, INR, to the unicode Rupee symbol ₹. Some messages attach numbers directly (Rs.500), others insert spaces (Rs 500.00). Indian number formatting uses lakhs and crores separators (1,50,000.00) instead of standard international thousand separators (150,000.00).
When developers first attempt to parse these messages, their instinct is to craft a single, massive regular expression to capture everything.
Here is why that approach fails, and how FinLedger architected a staged deterministic field extraction pipeline (DeterministicFieldExtractor.kt) that handles 50+ financial institutions without catastrophic backtracking.
The Pitfall of the "God Regex"
A monolithic regular expression attempting to capture the amount, account suffix, merchant, and reference code in a single pattern looks like this:
(?i)(?:Rs\.?|INR|₹)\s*([\d,]+\.?\d*).*?(?:debited|spent|paid).*?(?:A/c|Card|no\.?)\s*(?:ending|no\.?)?\s*[*X]+(\d{4}).*?to\s+([A-Za-z0-9\s]+)
This pattern suffers from two fatal engineering flaws:
flowchart TD
subgraph MonolithicFailure["The Fragility of the God Regex"]
Input["Incoming Bank SMS with slight syntactic variance"] --> Regex["Monolithic Regex Engine"]
Regex --> Backtracking["Catastrophic Backtracking (ReDoS):
Greedy and lazy quantifiers collide on long disclaimer texts.
CPU spikes to 100%, freezing the thread!"]
Regex --> Fragility["Fragile Group Capture:
Tweaking group 3 to support Kotak Bank breaks
merchant extraction for HDFC Bank!"]
Backtracking --> Failure["Silent Dropping of Valid Bank Alerts"]
Fragility --> Failure
end
1. Catastrophic Backtracking
When an incoming message contains unexpected footer text (such as fraud disclaimers or customer care hotlines), overlapping lazy quantifiers (.*?) force the regex engine to explore millions of permutation branches. On an Android device, this can lock up the background thread for hundreds of milliseconds, consuming battery and triggering ANR (Application Not Responding) warnings.
2. Coupling and Regression Fragility
When one regular expression tries to extract all four fields at once, modifying the pattern to accommodate an edge case in Bank A frequently breaks extraction for Bank B. Maintaining such a pattern becomes impossible as new bank formats are added.
The FinLedger Solution: Staged Deterministic Extraction
Instead of relying on a single monolithic regex, FinLedger decomposes parsing into a staged deterministic pipeline (DeterministicFieldExtractor.kt):
flowchart TD
subgraph StagedPipeline["FinLedger Deterministic Field Extraction Pipeline"]
RawSMS["Raw SMS String"] --> Step1["Stage 1: Polarity Extractor
Deterministic scan for Debit vs Credit signals"]
Step1 --> Step2["Stage 2: Sanitized Amount Extractor
Regex isolates numeric string;
Sanitizer strips Indian Lakh commas (1,50,000 -> 150000)"]
Step2 --> Step3["Stage 3: Account & Card Identifier
Targets masked suffixes (XX1234, **5678, ending 9012)"]
Step3 --> Step4["Stage 4: Counter-Party & UTR Extractor
Extracts recipient merchant and reference ID"]
Step4 --> Step5["Stage 5: Verification & Boundary Check
Ensures amount > 0 and account mask valid"]
Step5 --> CleanCandidate["Normalized ParsedTransactionCandidate"]
end
Let's examine how each stage works independently:
Stage 1: Polarity Isolation
The engine scans for unambiguous directional verbs:
-
Debit Signals:
debited,spent,paid,withdrawn,sent,transferred to,deducted. -
Credit Signals:
credited,received,deposited,refund,cashback,reversal.
If both signals appear (e.g., "Refund of Rs 500 credited for previously debited item"), the engine applies semantic precedence rules prioritizing refunds and reversals.
Stage 2: Indian Currency and Number Sanitization
Extracting amounts requires handling Indian comma placement (₹1,50,000.00).
- A specialized amount regex matches currency markers followed by numeric characters and commas.
- Once the substring is captured, the engine strips commas (
replace(",", "")) before passing the string toDouble.parseDouble(). - If the parsed amount is less than or equal to zero, or is infinite, the extraction is rejected as invalid.
Stage 3: Masked Account and Card Digits
Indian regulations mandate that banking alerts mask account and card numbers, revealing only the last 3 to 4 digits:
- Account patterns:
A/c\s*(?:no\.?)?\s*[*X]+(\d{3,4}) - Credit card patterns:
card\s*(?:ending\s*)?(?:in\s*)?[*X]+(\d{4})
By isolating the account extractor from the amount extractor, differences in where the bank places the account number (at the beginning of the text or at the end) have zero impact on amount extraction.
Stage 4: Merchant and UTR Reference Parsing
Once the amount and account are secured, the engine isolates the merchant:
- In UPI transactions, the merchant is extracted from VPA references (
to VPA ...orUPI Ref ...). - In POS transactions, the merchant is extracted after phrases like
at ...orinfo: .... - Cleaners strip common filler words such as
on,via,using, anddate.
Real-World Reliability Metrics
Decomposing the parser into isolated, single-responsibility stages transforms both performance and maintainability:
| Dimension | Monolithic God Regex | Staged Deterministic Extractor |
|---|---|---|
| Backtracking Risk | High (Exponential worst-case) | Zero (Linear $O(N)$ execution) |
| Parsing Latency | 2.5 ms – 25 ms | < 0.35 ms per message |
| Cross-Bank Interference | Severe (Fragile regression) | Zero (Completely decoupled) |
| Test Coverage | Difficult to isolate failures | 100% Parameterized Unit Testable |
Key Takeaway
When dealing with real-world, unstructured text, attempting to solve the entire problem in a single regular expression is an anti-pattern.
By breaking parsing into a sequence of isolated, deterministic stages—polarity, amount sanitization, account masking, and merchant extraction—FinLedger achieves flawless accuracy across 50+ banking formats while guaranteeing blazing performance on every Android device.
FinLedger is a privacy-first, offline-first personal finance application for Android that models bank accounts, credit cards, and cash without double-counting.
Explore FinLedger on Google Play: https://play.google.com/store/apps/details?id=com.devanshufinance.app

Top comments (1)
Separating amount extraction from account matching makes regression tests much easier to reason about. One detail worth tightening: parse money into integer minor units or a decimal type rather than Double, so rounding errors do not creep into later totals. How do you handle alerts that contain both a transaction amount and an available balance?