728x90

6.6. Subtraction Class

We can use subtraction to negate one or more character classes. For example, we can match a set of odd decimal numbers:

@Test
public void givenSetWithSubtraction_whenMatchesAccurately_thenCorrect() {
    int matches = runTest("[0-9&&[^2468]]", "123456789");
 
    assertEquals(matches, 5);
}Copy

Only 1, 3, 5, 7, 9 will be matched.

 

 

Let's dive to Pattern and Matcher

1.

if String qr has contains "substring value" like otpauth%3A%2F%2Ftotp%2F4009acd5fe30716b6201a93f5ed98999%253A35mwlee%2540naver.com%3Fsecret%3DMIHON7TZY7SEIH27RAOROQ7DI7LHCAJEDBLI4R6LSITWZSITS7PQN75S65E2TLEB%26issuer%3D%25EB%258D%2594%25EB%2593%25AC%25EC%259D%25B4%25EC%2599%2595%25ED%2594%25BC%25EC%25BD%259C%25EB%25A1%259C%26id%3Do87a66240433494aa362ad88e69af9c5

 

We can parse ID string like this

    try{
        String decodeStr = URLDecoder.decode(qr, "UTF-8"); // URL Decode
        Pattern pattern = Pattern.compile("id=([^&]+)"); // Standby ID substring
        Matcher matcher = pattern.matcher(decodeStr);	// Matcher Object will find the target data

        if(matcher.find()) {
            jsonUuid = matcher.group(1);
            log.debug("Extracted ID : {}", jsonUuid);
        } else {
            log.debug("ID not found");
        }
    } catch (UnsupportedEncodingException e){
        e.printStackTrace();;
    }

 

 

https://www.baeldung.com/regular-expressions-java

 

+ Recent posts