#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2016
    Posts
    3
    Rep Power
    0

    Regex by key & value


    hey
    I have encrypted code wrap with [],
    Code:
    [|kXS6k~R5I~Q5gHR&f3gzJ[X]
    How can i copy only the value ?
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    Be more specific. What "key" and "value"? What "encrypted code"? What part of the string are you trying to capture? What is the structure of the string? Are the |s and ~s significant? How about the [ at the end?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2016
    Posts
    3
    Rep Power
    0
    Ok, It's more complicated than I thought,

    first I have this encrypt method:
    Code:
        public String encrypt(String plain) {
            String b64encoded = Base64.getEncoder().encodeToString(plain.getBytes());
            String reverse = new StringBuffer(b64encoded).reverse().toString();
            StringBuilder tmp = new StringBuilder();
            final int OFFSET = 4;
            for (int i = 0; i < reverse.length(); i++) {
                tmp.append((char) (reverse.charAt(i) + OFFSET));
            }
            return tmp.toString();
        }
    It should encrypt Username + currentTimeMillis together -> called "token"
    from another class i get entrySet that addes brackets ,
    for example
    |kXS6k~R5I~Q5gHR&f3gzJ[X
    -->
    [|kXS6k~R5I~Q5gHR&f3gzJ[X]
    tryed this patten
    Code:
     \[([^\\]*)\]
    , but not match something like
    AA{R7QHQ8onQ~QXR7UXQzM\e{J6Y
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    Why use regular expressions at all? Use normal string processing to remove the leading [ and trailing ].

    Comments on this post

    • MaorS agrees
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2016
    Posts
    3
    Rep Power
    0
    Finally I wrote this:
    Code:
        public String getToken(String token) {
            token = token.replaceFirst("\\[", "");
            StringBuilder sb = new StringBuilder(token);
            return sb.replace(token.lastIndexOf("]"), token.lastIndexOf("]") + 1, "").toString();
        }

IMN logo majestic logo threadwatch logo seochat tools logo