0

I've a string like this, and I need to save each value in two strings:

Variable 1 = 385289
Variable 2 = L1S8dM4OW0VkGhKgl0NcGA%3d%3d

<<<385289<<<L1S8dM4OW0VkGhKgl0NcGA%3d%3d

Another example:

<<<383047<<<BBBIVvjjeNodJzLrIJqZaw%3d%3d

How can I do this with a regular expression in java ?

1
  • i assumed that you're trying to do joining. Please put the input sat very first and then put the expected output. Commented Apr 24, 2015 at 10:54

2 Answers 2

2

Use replaceAll() to extract the bit you want:

String var1 = str.replaceAll("<<<(.*?)<.*", "$1");
String var2 = str.replaceAll("<<<.*?<<<", "");
Sign up to request clarification or add additional context in comments.

2 Comments

why .*?, not .* ?
@SashaSalauyou a) it's faster (.* moves the pointer all the way to the end of input then backtracks 1 char at a time until a match is found), b) it .*? allows for <<< to appear as part of the second value - ie .*? stops consuming as soon as a match is found, but .* will potentially skip over <<< and match a later occurrence of <<<
0

You can try with this code:

Object Variable1;
Object Variable2;
String example = "<<<385289<<<L1S8dM4OW0VkGhKgl0NcGA%3d%3d";
String[] split = example.split( "<<<" );
for( String splittedString : split )
{
    if( splittedString.length() > 2 )
    {
        Variable1 = split[1];
        Variable2 = split[2];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.