Regex grab numbers after dollar sign - android

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);
}

Related

How to extract amount debited amount from SMS?

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.

Android Studio version of an Excel vlookup?

Ok I'm a massive noob and apart from following lots of tutorials I like to set myself a problem and then try to fix it with an app. Therefore I'm trying to make a little app that'll help me when I'm at work.
Basically it needs to breakdown a 4 character string into it's individual characters and then display them phonetically. So if I (the user) type in 5F9A then it'll display FIVE FOXTROT NINE ALPHA. At work we have an excel spreadsheet that does this all and I'm just trying to reverse engineer it. The spreadsheet itself has multiple stages, it reads the characters, converts them into ASCII and then performs a vlookup on a range of cells where each ASCII code is next to it's phonetic pronunciation. It looks for the number 53 (5 in ASCII) and then looks at the cell next to it which says FIVE.
I've managed to translate any user input into ASCII but I just don't know how to store and access this next set of data. I've been looking into SQLite but that is waaaaaay beyond me at the moment and seems far to complicated for something this simple?
Anyway, I know it's cheating asking for the answer, but maybe a push in the right direction?
The dummy way to do that would be:
Get every letter (char) of the word
Have a switch case that gives you the phonetic equivalent (you will have to do that by hand)
String word = yourWord;
String phonetic;
char currentChar;
for(i=0;i<=word.lenth();i++){
currentChar = word.substring(i, i+1);
phonetic = getPhonetic(currentChar)
}
String getPhonetic(char char){
switch char{
case a:
return alpha;
break;
case b:
....
}
}

How to select a word from a String

I'm working on a smart EditText where the user write a product and a price without any worries about formatting (e.g. "Pasta 0,50" or "0.50 tomato juice").
I'd like to separate the price from all the rest to have something like
String input = "Cheese 2,00 for Pizza";
String outputProduct = "Cheese for pizza";
String outputPrice = "2,00";
Usually it shouldn't be a thing, but I'm also considering of taking care if the price isn't at the very start or the very end of the string, so if it's in the middle.
I also have to deal with periods and comms in prices.. since not everyone use the same kind of format.
What I've tryed so far: answer from OscarRyz here but it seems to be not very related with what I'm trying to achieve.
Also seen some array operations but my problem is to get the actual length of the price.
Any help would be very appreciated!

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.

Float Precision Display (Android)

I am trying to make a program that takes some user input, runs a few calculations and outputs the answer. My problem is that this answer is sometimes many decimal places long which is causing some aesthetic and layout problems. I only need to display 4 decimal places worth of data. Is there anyway to limit the precision of these numbers at output time? (The Numbers are stored in floats and I'm programming for Android.)
You can format a float to 4 decimal places using String.format.
Example:
String result = String.format("%.4f", theNumber);
See also:
How to nicely format floating numbers to String without unnecessary decimal 0?
String.format(format, args)
Format strings in Java

Categories

Resources