Regex to extract 4-digit number from a String - Android [duplicate] - android

This question already has answers here:
Creating regex to extract 4 digit number from string using java
(8 answers)
Closed 6 years ago.
I need to extract a 4-digit number from a string:
e.g "Your otp for the login is 7832. This code will expire at 12:43:09PM" from SMS in Android
I want to extract 7832 or any 4-digit code that comes within the string. I ensure that there will be only one 4-digit code in the string.
Please help me. I trying to use patterns like:
str.matches(".*\\\\d+.*");
But I'm not able to understand regexes much.

String data = "Your otp for the login is 7832. This code will expire at 12:43:09PM";
Pattern pattern = Pattern.compile("(\\d{4})");
// \d is for a digit
// {} is the number of digits here 4.
Matcher matcher = pattern.matcher(data);
String val = "";
if (matcher.find()) {
val = matcher.group(0); // 4 digit number
}

Do:
\b\d{4}\b
\b matches word boundary
\d{4} matches 4 digits
Demo

Related

My currenct regex is not applying to my price [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
How to use regex to find a keyword in Kotlin
(2 answers)
Closed 25 days ago.
I'm trying to format a crypto currency price with the follow regex
fun Double.toPrice(): String {
val regex = Regex("(0.0*\\d{1,3})")
val match = regex.matches(this.toString())
return if(match) {
DecimalFormat(regex.pattern).format(this)
} else {
NumberFormat.getCurrencyInstance(Locale("en", "US")).format(this)
}
}
this regex works as expected as I saw in here https://regex101.com/r/0tFLiR/1
This regex should only get latest 3 numbers after x amount of 0 places after 0. in my numbers.
But this is not working as the if condition always yield false
how should I make this work ?
This is extracted from this answer in C# but I cannot make it work in my example

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.

How can i store more than 24,00,000 character in sqlite? [duplicate]

This question already has an answer here:
sqlite get field with more than 2 MB [duplicate]
(1 answer)
Closed 5 years ago.
I store Base64 String in sqlite and for that I use these data types TEXT, MEDIUMTEXT and LONGTEXT. But when I cross limit of 23,00,000 character, Android is not able to read data of the table. Can anyone help me?
You can always create code to break Strings apart at specific locations, and stitch it back together if you need to read the whole string.
String string = "abc def ghi jkl mno";
String ss[] = s.split(" ", 5);
// ss = {"abc", "def", "ghi", "jkl", "mno"};

Can't match string which stored in SQLite [duplicate]

This question already has answers here:
String comparison - Android [duplicate]
(17 answers)
Closed 6 years ago.
In part of my application I get date from mobile and store it in SQLite Database as string.
when I try to select rows from database which have same date, can't find any match result.
Also as in below code, I tried to get specific cell of table which I'M sure it's match the current date. but no match.
is there any idea?
Get Date from mobile and insert it as string.
String currentDateString = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
MyDB.insertRow(currentDateString);
Check if date from database equal mobile date.
if (MyDB.getRow(21).getString(1).toString()==currentDateString)
Toast.makeText(getBaseContext(),"Match",Toast.LENGTH_SHORT).show();
You don't match Strings in Java using == operator. You use .equals() method as String is not a primitive type.
So try this -
if (MyDB.getRow(21).getString(1).toString().equals(currentDateString))
Strings aren't matched by == operator because it is an object.
try
MyDB.getRow(21).getString(1).toString().equals(currentDateString)
and if you need it not case sensitive
MyDB.getRow(21).getString(1).toString().equalsIgnoreCase(currentDateString)
Also
make sure that the inserted date format is the same that you match with

Remove decimal point and numbers from string in Android [duplicate]

This question already has answers here:
How to remove the " .0" in a whole number when using double in java? [duplicate]
(3 answers)
Closed 4 years ago.
So, I have two numbers as strings - for example, 123.00 and 123.50. How do I remove the decimal point and all that follows. I had a method that worked for the 123.00, but would not work correctly on the 123.50.
Here is what I have so far:
String balance = getString("balance");
BigDecimal number = new BigDecimal(balance);
String formattedBalance = number.stripTrailingZeros().toPlainString();
int roundedBalance = Integer.parseInt(formattedBalance);
int roundedBalance = Integer.parseInt(getString("balance").split(".")[0]);
Do you want to remove the following points or numbers?
If only the points why not:
balance = balance.replace(".","");

Categories

Resources