I've a String from a SMS like:
"Your code: 123456. Your reference number is 012. The total amount is E 1250,00.."
Now I need to extract the code '123456', which is different every time, but always 6 digits.
How can I extract this 6 digit number from the string? Can I use Regex expressions somehow? I need to be sure, so I don't want to split on 'code: ' for example.
EDIT: The rest of the text can be different, but there will be only 6 digits in the SMS.
A short code is a 5 or 6 digit phone number that is used by organizations to send text messages at scale. People opt into SMS marketing programs by texting a word or phrase known as a "keyword" to a short code. They are specifically meant to be shorter than normal phone numbers to make the opt in process easier.
Solution #1: Padding Numbers with the TEXT function The following image shows how to write the TEXT formula to convert the value to a string that is 6 characters long. The “000000” is the number format that will convert the number to a 6 digit number. The zeros in the number format are placeholders for numbers.
The regexpr for a 6 digit number is
\d{6}
Edit: The code will look like
public static String extractDigits(final String in) {
final Pattern p = Pattern.compile( "(\\d{6})" );
final Matcher m = p.matcher( in );
if ( m.find() ) {
return m.group( 0 );
}
return "";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With