Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find a 6 digit number in String

Tags:

java

regex

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.

like image 930
Kapé Avatar asked Jan 04 '12 13:01

Kapé


People also ask

What is a text from a 6 digit number?

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.

How do I get 6 digit numbers in Excel?

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.


1 Answers

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 "";
 }
like image 111
mana Avatar answered Oct 30 '22 05:10

mana