1

I've two string token, I'm checking the pattern and send both the token back with ":" delimiter. Input :

String oldToken = "Bearer IdmH0VCifziTjHNGfyKfK9YCoEsy6nTDI";
String newToken = "Bearer IdsfdWeRrrfziTjHNGfyKfK9YCoEsy6nTDI";

Output:

IdmH0VCifziTjHNGfyKfK9YCoEsy6nTDI : IdsfdWeRrrfziTjHNGfyKfK9YCoEsy6nTDI

Please find my code below:

public class TestToken {
    public static  void main(String[] args) {
        String oldToken = "Bearer IdmH0VCifziTjHNGfyKfK9YCoEsy6nTDI";
        String newToken = "Bearer IdsfdWeRrrfziTjHNGfyKfK9YCoEsy6nTDI";
        String result = getToken(oldToken, newToken);
        System.out.println("result: " +result);
    }

    private static String getToken(String oldtoken, String newToken) {
        Pattern pattern = Pattern.compile(
                "^Bearer (?<token>[a-zA-Z0-9-._~+/]+)=*$",
                Pattern.CASE_INSENSITIVE);

        Matcher matcher =null;
        if (StringUtils.startsWithIgnoreCase(oldtoken, "bearer")) {
            matcher = pattern.matcher(oldtoken);
            boolean matchResult = matcher.matches();
        }
        return matcher.group("token");
    }
}

How can I combine both these token in this case and return it back. Any help would be really appreciated. Thanks!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Why use regex at all? And all of the rest to make sure you don't get "Bearer"? `str.split(" ", 2)[1]` gets you the token and ignores `Bearer`. Or at most just replace "Bearer" win the string with nothing and you still get the token. Then just concatenate the tokens with `:` between them. – VLAZ May 13 '21 at 14:34

2 Answers2

2

You can check match the pattern in both, oldtoken and newtoken and combine the matches.

Demo:

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

public class Main {
    public static void main(String[] args) {
        String oldToken = "Bearer IdmH0VCifziTjHNGfyKfK9YCoEsy6nTDI";
        String newToken = "Bearer IdsfdWeRrrfziTjHNGfyKfK9YCoEsy6nTDI";
        String result = getToken(oldToken, newToken);
        System.out.println("result: " + result);
    }

    private static String getToken(String oldtoken, String newToken) {
        Pattern pattern = Pattern.compile("^Bearer (?<token>[a-zA-Z0-9-._~+/]+)=*$", Pattern.CASE_INSENSITIVE);
        String result = "";
        Matcher matcher = pattern.matcher(oldtoken);
        if (matcher.find()) {
            result += matcher.group(1);
        }

        result += " : ";

        matcher = pattern.matcher(newToken);
        if (matcher.find()) {
            result += matcher.group(1);
        } else {
            result = "";
        }
        return result;
    }
}

Output:

result: IdmH0VCifziTjHNGfyKfK9YCoEsy6nTDI : IdsfdWeRrrfziTjHNGfyKfK9YCoEsy6nTDI
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

Without using regular expressions at all: split the string on space and take the second part. You can add a limit of 2 just in case you expect any other spaces later in the string.

private static String getToken(String oldtoken, String newToken) {
    String s1 = oldtoken.split(" ", 2)[1];
    String s2 = newToken.split(" ", 2)[1];

    return s1 + " : " + s2;
}

If you really wish to use regular expressions, it's better to do it for a case-insensitive replacement to remove "Bearer" from the front of the string:

private static String getToken(String oldtoken, String newToken) {
    String removePattern = "(?i)^Bearer\\s+";
    String s1 = oldtoken.replaceFirst(removePattern, "");
    String s2 = newToken.replaceFirst(removePattern, "");

    return s1 + " : " + s2;
}

This pattern might be overly generic with no use. It is case-insensitive and it also matches any amount of spaces after "Bearer". Using "^Bearer " as replacement pattern is very likely enough.


It is also possible to do the the above without using any regex at all by taking the substring after "Bearer ".

private static String getToken(String oldtoken, String newToken) {
    int removeBearer = "Bearer ".length();
    String s1 = oldtoken.substring(removeBearer);
    String s2 = newToken.substring(removeBearer);

    return s1 + " : " + s2;
}

You can also just hardcode the length, as there are 7 characters in that string and that is always going to be the case. But leaving it there makes it easy to see why 7 is chosen.

VLAZ
  • 26,331
  • 9
  • 49
  • 67