Home > OS >  RegExp how to extract amount from a String
RegExp how to extract amount from a String

Time:08-03

I have a string

String str = "Rs.50000.00 paid thru A/C XX3380 on 28-6-22 16:21:15 to ---, UPI Ref ----. If not done, SMS BLOCKUPI to ----.-Canara Bank"

I want to Extract that amount Rs. 50000.00 This RegExp can get this amount = RegExp(r"\b\d \.\d \b")

I used this code

final intString = _filteredMessages[1].body!;
  final reg = RegExp(r"\b\d \.\d \b");
  final str = reg.allMatches(intString).map((e) => e.group(0));

Giving me output (50000.00)

And i have hard time removing the parenthesis

2 Questions

  1. How can extract amount from the string, need just the int without the parenthesis.
  2. How to remove the parenthesis from (50000.00)

CodePudding user response:

The parentheses are there because you are creating an iterable of all matches. If your regex had more matches it would be like (50000.00, 1000.00) for example. I believe you just want the first match and in that case you can do

final str = reg.firstMatch(intString)?.group(0);

CodePudding user response:

  1. This is best according to me just split the text before paid and get the value you want.

     final amount = str.split("paid");
     var prefix = amount[0].trim(); 
     print(prefix);  //Rs.50000.00
    
  • Related