I hope you're doing well.
I'm trying to make a regex for matching hexadecimal string in a message.
Sometimes the hex string is normal, always at format 0x 40 hex characters like this : 0x0D6185e7481968D82B51F07191F8dAa721b2Db10
.
But sometimes, there is some spaces within it : 0x0 D6185 e7481968D82B51F07191F8 dAa721b2Db10
And sometimes, this hex string is embedded in a message, so I can't use basic expression like /([a-fA-F0-9x]{3,})/gm
, because in the following example
access
0x0D 6185e7481968 D82B51F07191F8d Aa721b2Db10
the acce
part will also be selected when I'll try to reconstruct the hex string.
Generally the hex string is only on one line, but some other times on two or more lines like this :
access
0x0D6185e7481968
D82B51F07191F8d
Aa721b2Db10
I can't figure out how I can solve this problem, so if someone could help me I would be grateful.
Thanks for reading,
Mateo
CodePudding user response:
You can use
/0x(?:\s*[a-fA-F0-9]){40}/g
See the regex demo.
Details:
0x
- a0x
fixed string(?:\s*[a-fA-F0-9]){40}
- forty repetitions of zero or more whitespaces and then a hex char.
See a JavaScript demo:
const text = "0x0D6185e7481968D82B51F07191F8dAa721b2Db10\nBut sometimes, there is some spaces within it : 0x0 D6185 e7481968D82B51F07191F8 dAa721b2Db10\n\naccess\n0x0D6185e7481968 \nD82B51F07191F8d\nAa721b2Db10";
const regex = /0x(?:\s*[a-fA-F0-9]){40}/g;
console.log( text.match(regex) )