7 回答

TA貢獻1804條經驗 獲得超2個贊
你可以這樣得到otp。
String allNum=message.replaceAll("[^0-9]",""); String otp=allNum.substring(0,6);

TA貢獻1799條經驗 獲得超6個贊
您可以從任何消息中提取任意 6 位數字String?!皘” 用于查找更多可能的組合。只有“\d{6}”還可以為您的問題提供正確的結果。
//find any 6 digit number
Pattern mPattern = Pattern.compile("(|^)\\d{6}");
if(message!=null) {
Matcher mMatcher = mPattern.matcher(message);
if(mMatcher.find()) {
String otp = mMatcher.group(0);
Log.i(TAG,"Final OTP: "+ otp);
}else {
//something went wrong
Log.e(TAG,"Failed to extract the OTP!! ");
}
}

TA貢獻1776條經驗 獲得超12個贊
String message="OTP 為 145673,并且在接下來的 20 分鐘內同樣有效";
System.out.println(message.replaceFirst("\d{6}", "******"));
我希望這有幫助。

TA貢獻1936條經驗 獲得超7個贊
如果您的消息始終以“您的 OTP 代碼是:”開頭,并且在代碼后有換行符 (\n),則使用以下內容:
Pattern pattern = Pattern.compile("is : (.*?)\\n", Pattern.DOTALL);
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
Log.i("tag" , matcher.group(1));
}

TA貢獻1824條經驗 獲得超5個贊
使用這樣的正則表達式:
public static void main(final String[] args) {
String input = "Your OTP code is : 123456\r\n" + "\r\n" + "FA+9qCX9VSu";
Pattern regex = Pattern.compile(":\\s([0-9]{6})");
Matcher m = regex.matcher(input);
if (m.find()) {
System.out.println(m.group(1));
}
}

TA貢獻1830條經驗 獲得超3個贊
嘗試一下希望這會對您有所幫助。
String expression = "[0-9]{6}";
CharSequence inputStr = message;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
添加回答
舉報