简体   繁体   English

Java正则表达式查找十六进制字符串

[英]Java Regex to find Hex String

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. 我有一个包含\\ x76 \\ x6f \\ x69 \\ x64 \\ x28 \\ x29(void())表示形式的十六进制值的字符串,我想从上述字符串中搜索\\ x76 \\ x6f \\ x69 \\ x64 \\ x28 \\ x29值。

needed java regex pattern for same. 同样需要Java正则表达式模式。

thanks in advance. 提前致谢。

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+) .*$ , ie, matching the sequence several times, capturing it (in $1 ), and discarding the rest of the string. \\\\x[0-9a-f]{2} (对于Java字符串应正确转义)与一个字符序列匹配,然后从那里开始就是^(sequence+) .*$ ,即多次匹配该序列,将其捕获(在$1 ),并丢弃字符串的其余部分。

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). 如果不是这种情况,则必须删除^ (与字符串的开头匹配)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM