Reputation: 1
I had a string contains hex value like \x76\x6f\x69\x64\x28\x29 (void()) representation, i would like to search the \x76\x6f\x69\x64\x28\x29 value from above string.
needed java regex pattern for same.
thanks in advance.
Upvotes: 0
Views: 1367
Reputation: 44023
Since this appears to be some sort of debugger output, I'm assuming that you mean a string with escape sequences rather than the characters these escape sequences represent. In that case:
String s = "\\x76\\x6f\\x69\\x64\\x28\\x29 (void())";
String s2 = s.replaceAll("^((\\\\x[0-9a-f]{2})+) .*$", "$1");
Where \\x[0-9a-f]{2}
(properly escaped for Java strings) matches a character sequence, and from there it's just ^(sequence+) .*$
, i.e., matching the sequence several times, capturing it (in $1
), and discarding the rest of the string.
This assumes that the string starts with the sequence of character sequences. If this is not the case, you will have to remove the ^
(which matches the beginning of the string).
Upvotes: 1