Add leading zeros between "0x" and comma, to make a 8-digit hex number
-
Hello,
I have a huge list of number that look like this:
{0x1023350, 0x3014},
{0x1023954, 0x3007},
{0x1023960, 0x10F},
{0x102396C, 0x2FF},
{0x10219, 0x16},The output I want from this is all the above Hex numbers formatted to 8-digits using zero padding as follows:
{0x01023350, 0x00003014},
{0x01023954, 0x00003007},
{0x01023960, 0x0000010F},
{0x0102396C, 0x000002FF},
{0x00010219, 0x00000016},How can I do this using regular expression ? Appreciate any help
-
Try:
Find what box:
0x(([[:xdigit:]]{7}\b)|([[:xdigit:]]{6}\b)|([[:xdigit:]]{5}\b)|([[:xdigit:]]{4}\b)|([[:xdigit:]]{3}\b)|([[:xdigit:]]{2}\b)|([[:xdigit:]]{1}\b))
Replace with box:0x(?{2}0)(?{3}00)(?{4}000)(?{5}0000)(?{6}00000)(?{7}000000)(?{8}0000000)${1}
Search mode radiobuttons: pressRegular expression
There are simpler expressions; this was just a fast one I came up with.
Learn to answer questions of this kind yourself in the future; start reading HERE and also check out the N++ user manual’s searching section HERE.
-
@Alan-Kilborn said in Add leading zeros between “0x” and comma, to make a 8-digit hex number:
0x(?{2}0)(?{3}00)(?{4}000)(?{5}0000)(?{6}00000)(?{7}000000)(?{8}0000000)${1}
Hi Alan,
Thank you . That did work. Although I must spend sometime later to understand how this works :) -
@Sankar-S said in Add leading zeros between “0x” and comma, to make a 8-digit hex number:
Although I must spend sometime later to understand how this works
Alan linked you to the main group, but two of the concepts involved are:
- Capture Groups (FIND): https://npp-user-manual.org/docs/searching/#capture-groups-and-backreferences
- Conditional Substitution (REPLACE): https://npp-user-manual.org/docs/searching/#substitution-conditionals
-
@Sankar-S said in Add leading zeros between “0x” and comma, to make a 8-digit hex number:
Although I must spend sometime later to understand how this works
Additionally, if you were to visit https://regex101.com/r/nxiyAH/1 you will see an explanation of @Alan-Kilborn’s regular expression (regex) and colour coded selections on your example.
This site does an excellent job of explaining some regex, but do be careful as the flavor (of regular expressions) Notepad++ uses has some subtle differences.
It is just another tool in the arsenal of a lot of regex coders.
Terry
-
@PeterJones @Terry-R , Thank you , I will check out these resources .