Search code examples
javaregexalphanumericone-time-password

Regex to retrieve any alphanumeric otp code


I am trying to get a regex to retrieve alphanumeric OTP code (length of the code maybe dynamic i.e depending on user's choice) and must contain at least one digit.

I have tried the following regex : "[a-zA-z][0-9].[a-zA-z]"

But if a special character is there in the code it should result null instead it retrieves the characters before and after the special character which is not desired.

Some sample OTP-containing messages on which the regex is desired to work successfully:

  1. OTP is **** for txn of INR 78.90.
  2. **** is your one-time password.
  3. Hi, Your OTP is ****.

Examples of Alphanumeric OTPs with at least one-digit:

  1. 78784
  2. aZ837
  3. 987Ny
  4. 19hd35
  5. fc82pl

Solution

  • It would be a bit difficult, maybe this expression might work with an i flag:

    [a-z0-9]*\d[a-z0-9]*
    

    or with word boundaries:

    (?<=\b)[a-z0-9]*\d[a-z0-9]*(?=\b)
    

    Test

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    final String regex = "[a-z0-9]*\\d[a-z0-9]*";
    final String string = "78784\n"
         + "aZ837\n"
         + "987Ny\n"
         + "19hd35\n"
         + "fc82pl";
    
    final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
    final Matcher matcher = pattern.matcher(string);
    
    while (matcher.find()) {
        System.out.println("Full match: " + matcher.group(0));
        for (int i = 1; i <= matcher.groupCount(); i++) {
            System.out.println("Group " + i + ": " + matcher.group(i));
        }
    }
    

    The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.