Leading Zeros
-
@Jose-Emilio-Osorio, I know I’ve seen somewhere on the web a great regex to do the zero padding, but cannot find it now. So I’ve had to make my own. It’s a bit rough but hopefully does the job you need. Both of the expressions I’m providing will be entered under the Search\Replace menu option (Ctrl-H). You need “Wrap around” ticked and Search Mode set to “Regular Expression”, nothing else should be selected.
For zero padding:
Find what:(\d{8,})|(\d{7})|(\d{6})|(\d{5})|(\d{4})|(\d{3})|(\d{2})|(\d)
Replace with:(?{1}\1)(?{2}0\2)(?{3}00\3)(?{4}000\4)(?{5}0000\5)(?{6}00000\6)(?{7}000000\7)(?{8}0000000\8)
This expression doesn’t care where the number is located, in other words ALL numbers will be changed. If that is not what you want then you will need to identify the parameters in which numbers can be changed.For the trailing spaces question:
Find what: (\d{12,})|(\d{11})|(\d{10})|(\d{9})|(\d{8})|(\d{7})|(\d{6})|(\d{5})|(\d{4})|(\d{3})|(\d{2})|(\d) Replace with: (?{1}$1)(?{2}$2 )(?{3}$3 )(?{4}$4 )(?{5}$5 )(?{6}$6 )(?{7}$7 )(?{8}$8 )(?{9}$9 )(?{10}$10 )(?{11}$11 )(?{12}$12 )
Again it will work on ALL numbers.
You should be able to copy and paste the text above, this will eliminate errors in typing.
in both cases they finish with the last ‘)’. This system uses a ‘markdown’ interpreter and it may be interfering with the number of spaces in the replace text for trailing spaces (I have embedded it in a window, I think?). So once you have copied that through, count them. The one with $12 should have 11 spaces, the $11 has 10 spaces going back down the line with $2 having 1 space.I hope they help, this was quite a job but enjoyable, I’m learning some new things as well.
Terry
-
The beauty of regex solution is that it runs out of the box without using any plugins.
Personally I think reformatting a string can’t be a achieved more easily by using scripting langauges.
So just in case someone uses python script plugin these, I assume, would be the commands needed.to fillup up to 8 digits with 0 in front of a number
editor.rereplace('\d+',lambda m: '{:>08}'.format(m.group(0)))
to add spaces
editor.rereplace('\d+',lambda m: '{:<12}'.format(m.group(0)))
both in one go if needed
editor.rereplace('\d+',lambda m: '{:<12}'.format('{:>08}'.format(m.group(0))))
Cheers
Claudia -
@Terry-R and @Jose-Emilio-Osorio :
FYI, this thread also discusses a way to leading-zero-pad or leading-space-pad numbers, also using regular expressions.
For trailing space-padding, although I’m sure it can be done with a regex replacement, I’d be tempted to use the mouse and alt+left-click somewhere to the right of the longest line, then drag to get a “skinny” caret on the lines to be affected, then hit the spacebar, then backspace off any undesired common trailing space. Whew. That was a lot to explain, maybe a video (with visible whitespace turned ON) makes it clearer how this could be done:
-
@Claudia-Frank said:
Personally I think reformatting a string can’t be a achieved more easily by using scripting langauges.
Claudia, I think what you meant actually was: “Personally I think reformatting a string CAN be achieved more easily by using scripting languages.”
And I agree…but there is that hurdle you mentioned: Requiring a plugin someone might otherwise not be using.
-
Thx Scott, yes, of course CAN - aarrggghhh :-D
About the hurdle - EVERYONE should use python script plugin - just kidding :-DCheers
Claudia -
@Scott-Sumner, yes that was the regex I recalled. So it was in my backyard, not way out yonder. I should have thought all the best ideas come thru this forum! I remember reading through old posts here when I first joined, but a lot of water under the bridge since then!
It does raise an interesting idea. Does/could this forum have a “Regex Repository” where all great regex’s could be stored? I suppose something akin to the FAQ. There have been a few like in this instance where the regex is “universal”, meaning easily adapted to suit the need. It would be nice to store in some easily viewable storage area. It would need a moderator and perhaps a voting system to allow for it’s inclusion, so not everyone could add a post in that area. Just a thought.
Terry
-
Thanks !!!
-
@Scott-Sumner Use the mouse is good. But, I have more than 85000 lines in a file and it´s hard to drag the line to the last line. Any method to do this ?. Thanks for your help.
-
more than 85000 lines in a file and it´s hard to drag the line to the last line. Any method to do this
Although there is a similar technique which avoids using the mouse, with that many lines Notepad++ gets really slow with it, so let’s go a different route. We’re going to do a 2-step regular expression replacement:
Step 1
Find what zone:
$
Replace with zone: put a bunch (20 or so?) of spaces here
Search mode: Regular expressionStep 2
Find what zone:
(?-s)^(.{12}).*
<— adjust12
here to be how many total columns you need in your output
Replace with zone:\1
Search mode: Regular expressionI tried it and Notepad++ 32-bit is a bit slow with those replacements for ~85000 lines, but just give it time without doing anything else with N++…it should finish.
Of course, regular expression replacement was mentioned earlier as a solution, but maybe this simpler one (two) is less daunting.
-
@Jose-Emilio-Osorio said:
@Scott-Sumner Use the mouse is good. But, I have more than 85000 lines in a file and it´s hard to drag the line to the last line. Any method to do this ?. Thanks for your help.
If it’s a one-shot deal then use two search/replaces in regular expression mode (Ctrl-G in the search/replace box)
Search for:
^
Replace with:00000000
If you have leading spaces then use search for:^ *
That’ll put eight zeroes at the front of all lines
The second search/replace is:
Search for:^[0]+([0-9]{8})$
Replace with:\1
That skips over excess leading zeroes, saves the last eight digits, and the replace part is the saved value.
Now do one more pass which is to search for
^0{8}
This searches for lines that start with eight zeroes.If you find any then either
- There were blank lines in the file and we now have 00000000 there. That may or may not be desirable
- One of the lines had a number with 9 or more digits and will now have something like 00000000123456789
- One of the lines had something other than a number and now has 00000000~whatever was on the line~
If you need to do this a lot then use sed. Google for GNU sed. It’s free. In sed the search/replaces would look like this
sed -r -e "s/^/00000000/" -e "s/^0+([0-9]{8})$/\1/" input-file.txt >output-file.txt
Then to check for errors
sed -r -n -e "/^0{8}/ { = ; p }" output-file.txt
This scans and for every line found that starts with eight zeroes it outputs the line number and then the offending line.