-3

I am developing Android App to read OTP from SMS and give user option to directly copy the OTP in clipboard.

Now problem is SMS can be as follow:-

  1. Time: 02:30 PM Your OTP is 3242. (otp can be of any digits).
  2. 12345 is your OTP. TIME: 04:45 AM.
  3. Your OTP for account 123456789 is 4565.

I need help to extract otp using Pattern and Matchers in java. or any other simple solution to get otp. Third case can be ignored. It will also work if we can get 4 or 5 or 6 digit numbers from SMS using Pattern and Matchers.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
Ameer ali khan
  • 525
  • 5
  • 8

1 Answers1

0

You can use this

This will cover all the three cases you given in example.

^\d+(?=\sis)|(?<=is\s)\d+\.?$
  • ^\d+(?=\sis) - Matches any digit followed by space and is. (^ anchor to start of string). Covers condition of 2nd example.
  • (?<=is\s)\d+\.?$ - Matches any digit preceded by is and space. ($ anchor to end of string). covers 1st and last example

Demo

Code Maniac
  • 33,907
  • 4
  • 28
  • 50