package eu.livotov.labs.android.d3s;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utilities to find 3DS values in ACS webpages.
 */
final class D3SRegexUtils {

    /**
     * Pattern to find an html tag with an attribute named PaRes.
     */
    private static final Pattern paresFinder = Pattern.compile(".*?(<input[^<>]* name=\"PaRes\"[^<>]*>).*?", Pattern.DOTALL);

    /**
     * Pattern to find the value from an html tag with an attribute named value.
     */
    private static final Pattern valuePattern = Pattern.compile(".*? value=\"(\\S+?)\"", Pattern.DOTALL);

    /**
     * Finds the PaRes in an html page.
     *
     * @param html String representation of the html page to search within.
     * @return PaRes or null if not found
     */
    @Nullable
    static String findPaRes(@NonNull String html) {
        if (html.trim().isEmpty()) return null;

        String paResTag = null;
        Matcher paresMatcher = paresFinder.matcher(html);
        if (paresMatcher.find()) {
            paResTag = paresMatcher.group(1);
        }

        if (paResTag == null) return null;

        String paRes = null;
        Matcher paresValueMatcher = valuePattern.matcher(paResTag);
        if (paresValueMatcher.find()) {
            paRes = paresValueMatcher.group(1);
        }

        return paRes;
    }
}

@@ -174,337 +174,306 @@
 n;%0A%0A
-/**%0A * Utilities to find 3DS values in ACS webpages.%0A */%0Afinal class D3SRegexUtils %7B%0A%0A    /**%0A     * Pattern to find an html tag with an attribute named PaRes.%0A     */%0A    private static final Pattern paresFinder = Pattern.compile(%22.*?(%3Cinput%5B%5E%3C%3E%5D* name=%5C%22PaRes%5C%22%5B%5E%3C%3E%5D*%3E).*?%22, Pattern.DOTALL);%0A%0A    /**%0A     * Pattern to find the
+import static java.util.regex.Pattern.CASE_INSENSITIVE;%0Aimport static java.util.regex.Pattern.DOTALL;%0Aimport static java.util.regex.Pattern.compile;%0A%0A/**%0A * Utilities to find 3DS values in ACS webpages.%0A */%0Afinal class D3SRegexUtils %7B%0A%0A    /**%0A     * Pattern to find the value of an attribute named
  val
@@ -516,21 +516,41 @@
 e named 
-value
+name and a value of PaRes
 .%0A     *
@@ -588,31 +588,22 @@
 ern 
-valuePatt
+paresFind
 er
-n
  = 
-Pattern.
 comp
@@ -611,46 +611,90 @@
 le(%22
-.*? value=%5C%22(%5C%5CS+?)%5C%22%22, Pattern.DOTALL
+%3Cinput(?=.+?name=%5C%22PaRes%5C%22)(?=.+?value=%5C%22(%5C%5CS+?)%5C%22).+%3E%22, DOTALL %7C CASE_INSENSITIVE
 );%0A%0A
@@ -1009,19 +1009,16 @@
 ng paRes
-Tag
  = null;
@@ -1128,19 +1128,16 @@
    paRes
-Tag
  = pares
@@ -1169,248 +1169,8 @@
  %7D%0A%0A
-        if (paResTag == null) return null;%0A%0A        String paRes = null;%0A        Matcher paresValueMatcher = valuePattern.matcher(paResTag);%0A        if (paresValueMatcher.find()) %7B%0A            paRes = paresValueMatcher.group(1);%0A        %7D%0A%0A
     
@@ -1194,9 +1194,8 @@
 %0A    %7D%0A%7D
-%0A

Combine tag and value regex for PaRes