How to extract amount debited amount from SMS? - android

I want to extract the debited amount from the SMS
my sms content is
Cash withdrawal of Rs3,000.00 made on Kotak Debit Card X2694 on 08-01-2020 at #Acoount Number#.Avl bal is Rs 110.32.Not you?Visit kotak.com/fraud. Transactions on non-Kotak ATMs are chargeable beyond 5 per month(3 for Metro locations), if applicable, as per your account variant. Visit www.kotak.com for details.

As suggested by Anunay, regex would solve your issue with a single line.
Personally, I'm a newbie to regex. A naive method I could suggest is to parse the string based on the occurrence of "Rs" and ".".
Then, remove all the (,)commas and convert the String to a float.
messageString = 'Your message as a String'
startIndex = messageString.index('Rs') + 2
endIndex = messageString.index('.') + 3
debitAmount = float(messageString[startIndex: endIndex].replace(',',''))
print(debitAmount)
Sorry, for my Python implementation.

Related

How to send a USSD code containing decimal floating point (.)?

I need to send a USSD code containing a double value, that represents the balance account amount to be transferred. This value is composed by an integer number, and optionally a decimal separator and 2 more digits. My code looks as follows:
double doubleValue = 0.70;
String phoneNumber = "51234567", pincode = "1234";
String ast = Uri.encode("*");
String baseUssd = ast + "234" + ast + "1" + ast + phoneNumber + ast + pincode + ast;
StringBuilder builder = new StringBuilder();
builder.append(baseUssd);
builder.append(doubleValue); //i.e: 1.35, 0.80
builder.append(Uri.encode("#"));
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + builder.toString()));
startActivity(intent);
My phone treats the doubleValue as 135, 080, etc. ignoring the dot separator character. I hope the final code includes "dot", allowing send the decimal value. Someone solved this problem?
The Java code shown works fine of course assuming that doubleValue is a float or a double.
As suggested here the Intent is handled by OutgoingCallBroadcaster.processIntent() which processes the String given in the Intent by calling PhoneNumberUtils.convertKeypadLettersToDigits() and PhoneNumberUtils.stripSeparators().
The latter one strips everything except numbers, *, #, + and the WILD, WAIT and PAUSE symbols.
This is where your decimal separator is lost.
So either the separator should be escaped to a specific numerical value or substituted by one of the accepted symbols to actually leave your phone and reach the receiver.
Whoever is responsible for the receiving end can probably advice you on properly formatting your decimal number.
Thinking about the way the pinpad, which my bank sent me, works, you always have to enter the two digits after the decimal point and the formatting on the display deals with the position of the point.
So if i enter "1", it is interpreted as 0.01.
Similarly "1023" would be 10.23.
I think the same approach could work nicely for you.
So 1.23 is entered as "123" and 0.80 as "80"
I can't see a reference that limits the characters to 0-9#* but all the examples follow this format. However, your example starts *234, which seems to fit this rule in the specification
Case a) 1, 2 or 3 digits from the set (*, #) followed by 1X(Y), where X=any number 0-4, Y=any number
0-9, then, optionally "* followed by any number of any characters", and concluding with # SEND:
This case is reserved for HPLMN use. When a serving network receives such a message from a
visiting subscriber, it shall pass the USSD message directly to the HPLMN. If it receives it from a
home subscriber, it is up to the network to decide whether to treat it locally or to pass it to the HLR
http://www.etsi.org/deliver/etsi_ts/100600_100699/100625/07.00.00_60/ts_100625v070000p.pdf
In general, I am not sure the HPLMN (Home Public Land Mobile Network) or HLR (Home Location Register) would expect the extra characters, even though the whole character set and even other character sets are allowed in the USSD protocol.

Android SMS unicode detect

I would like to know how to detect what coding has been used for SENT SMS (in case I read it from "content://sms/sent"). I check all columns and I didnĀ“t find any information about it there.
Reason why I am looking for this is to know, how many messages has been sent. I believe when it is sent as: 160 GSM characters = 1 SMS ; 70 Unicode characters = 1 SMS
Columns:
_id
thread_id
address
person
date
date_sent
protocol
read
status
type
reply_path_present
subject
body
service_center
locked
error_code
seen
deletable
hidden
group_id
group_type
delivery_date
app_id
msg_id
callback_number
reserved
pri
teleservice_id
link_url
Thank you for any help in advance.
I haven't tried it yet, but I assume you must check BOM marker in a text if you want to relate to its encoding. Try to read this answer: this
the SmsMessage.calculateLength method does that pretty well !
read the message's body string from the sms content provider
throw that to calculateLength(body, true)
read the results :
result[0] = 'number of SMS's required'
result[1] = 'number of code units used'
result[2] = 'number of code units remaining until the next message'
result[4] = 'encoding type that should be used for the message'
Cheers !

How can I avoid app names in inapp item titles returned from Google Play getSkuDetails?

I'd like the use the getSkuDetails() call from the In-app Billing v3 API to dynamically display a list of inapp purchase options with properly translated titles and relevant price.
However, the "title" property from getSkuDetails() seems to always be of the form "<item title> (app name)", which is less than useful. How can I get only the item title itself without the app name without hacking the string?
That is the way it is. I mean even I didn't like it, obviously user knows that he is buying from the app but I think Google is going to reply it in this way only
As no one has replied with an actual regex pattern to match the app name in parentheses in the SKU title I thought I just post the code here for further reference:
// matches the last text surrounded by parentheses at the end of the SKU title
val skuTitleAppNameRegex = """(?> \(.+?\))$""".toRegex()
val titleWithoutAppName = skuDetails.title.replace(skuTitleAppNameRegex, "")
The regex is as strict as possible to allow for additional text in parentheses within your SKU title without removing it as well (e.g. SKU titles like Premium (Subscription) would stay as they are). The only thing you should avoid is parentheses in your app name, but with a little tweaking of the regex you could work around that as well.
As regexes are notoriously expensive to build it is advisable to store it in a field and avoid constructing them each time over when you are parsing your SKUs.
Adapting #ubuntudroid answer to Java, I made it work like this:
String skuTitleAppNameRegex = "(?> \\(.+?\\))$";
Pattern p = Pattern.compile(skuTitleAppNameRegex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(skuDetails.getTitle());
String titleWithoutAppName = m.replaceAll("");
String productTitleWithAppName=skuDetails.getTitle();
String productTitleWithoutAppName = productTitleWithAppName.substring(0, productTitleWithAppName.indexOf("("));
You don't need to use regex for this. The following should work. This is in C# but you can guess how you can do it in Java or any other language.
string title = getTitle(); // get title
int appNameStartIndex = title.LastIndexOf("("); // find last index of (.
string titleWithoutAppName = title.Substring(0, appNameStartIndex); // and :)
In fact, the name of the SKU without the app name in parantheses is transfered with the SKU, but there is no getter to retrieve it.
If you have a deeper look into the implementation of SkuDetails class, then you see, that this whole thing is based on a json String (also debugging gives you this json).
And this json string contains not only the title with the apps name, but also a name field with the SKUs name only.
The SKUs json representation can be retrieved with SkuDetails.getOriginalJson().
So if it better fits your needs, then you can of course retrieve the SKU name directly from the data, returned from Google.
I found that in development builds, I'd get a value like My product (com.test.myapp (unreviewed)). Here's a version of the regex that handles the nested parentheses and keeps other parentheses in your product name intact:
const removeAppNameFromProductTitle = (title: string) => {
const regex = /( \([^()]*\)$)|( \([^)]*\)\)$)/im;
return title.replace(regex, '');
};
The code is in TypeScript (React Native), but I'm sure you can adapt. Here's a gist with test cases: https://gist.github.com/thmsobrmlr/732ecf958f600ec38e89c4e8ff57f3dd.

Regex grab numbers after dollar sign

I have dynamic strings that sometimes will have a price in them and sometimes not. Its for craigslist so there is really no set format there can be commas, dashes, etc. Here are some examples of a typical string:
Xbox 360 (black) Elite Console 120GB (Mason City Illinois ) $200
$200 2013 North Trail Camper (RT 202. Manchester, Maine) $224/mo.
Snowmobile Bike trailers (Winthrop / Augusta) $40 Monthly
"Great Xmas Gift" XBox 360 Guitar Hero (Springfied) $80
I am trying to split up the string into title, location, and price. I can grab the location with:
Pattern p = Pattern.compile("\(([^]*)\)");
Matcher m = p.matcher(title);
Having trouble figuring out how to separate the title and price out. Any help would be appreciated.
To grab number after $ use: \$[0-9]+ regex.
Personally I would use:
\$[-0-9.,]+[-0-9.,a-zA-Z]*\b
It will take quite a few non-numbers, but it will also glob up stuff like negative values, $1,000, $1mil and so on. The \b at the end will ensure it globs up as much as possible before a space or new line or something.
To grab number after $ use \$(\d+) regex.
Regex Demo
Note the capturing parentheses, the desired value will be accessible via m.group(1).
Double escape backslashes in Java code, too.
See snippet below:
Pattern p = Pattern.compile("\\$(\\d+)");
Matcher m = p.matcher(title);
String output = "";
if (m.find()) {
output = m.group(1);
}

How Match Regular expression of international number in android

i have a EditText in my activity where i take a phone number from user.. Now user provides a number activity and based on xml rules you i have to tell whether application routes the phone number or not.
This is more of a java question, you need to know how to build regular expressions. So I assume you know how to fetch the value of the edit text. Lets assume you have loaded the phone number in a String phoneNumber. So now how do you check against a regular expression:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
....
Pattern pattern = Pattern.compile("\+0[8-9][0-9]{2}-[0-9]{2}-[0-9]{3}");
Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.matches()) {
// The phone number matches the template given. do the routing.
}
In th eexample I have given I am searching for phone numbers that start with + (note that + is a special character for regexes, thus I need to escape it, the same holds for $, ., ^), then I expect a zero, 8 or 9, then exactly 2 digits, a dash 2 more digits and 3 more digits. The if matcher.matches() will return true only if the phoneNumber is exactly of the described format. Hopefully this will give you a brief introduction to the regex power of java.
ITU: National Numbering Plans can help you with the harder problem.
Your question is poor enough that any one of my link, Boris's regex-centric answer, or an answer that focused on nothing more than Android's GUI could be what you're really looking for. Please keep this in mind for future questions.

Categories

Resources