Finding and replacing characters
I am struggling to find a way to replace characters for sorting purpose.
for example, I want to replace the following characters:
where one zero is added after the letter “A” whenever there are three numbers and two zeros are added after “A” whenever there are three numbers.
Also, nothing should be done if there are four numbers after “A”.
I really appreciate it if you can help me.
PeterJones last edited by PeterJones
You will need to use capture groups and conditional substitutions. I have also added negative lookahead and negative lookbehind assertions, though it can probably be done without them, because I wanted to limit it to 1-4 digits.
You weren’t 100% clear as to whether these 1-4 digits would always be after an
A, or always after a letter, or always starting in the second column of the line, or some combination, so I made mine restrict it to any group of 1-4 digits, anywhere on the line, as long as there isn’t a digit before or a digit after (which would have meant it was really 5+ digits).
SEARCH MODE = Regular Expression
A1 A36 A282 A1482
A0001 A0036 A0282 A1482
Basically, the logic puts each individual digit after the first digit into a numbered group (1-3), as long as it’s exactly 1 thru exactly 4 digits with non-digits surrounding; then the replacement will say “if there isn’t a digit for the Nth group, use a
0at the beginning instead” 3 times, for the three optional digits – so it will insert no
0s before a four-digit number, one
0before a three-digit number, two
0before a two-digit number, and three
0before a one-digit number.
If you need it to be more restrictive, you will need to be more clear about examples that shouldn’t match; if you need it to be more permissive, you need to give more examples of data that should match that my expression does not. In other words, my answer is only as good as the example you gave and the assumptions I made (intentionally or unknowingly) based on the example data you provided.
- Please Read Before Posting
- Template for Search/Replace Questions
- FAQ: Where to find regular expressions (regex) documentation
- Notepad++ Online User Manual: Searching/Regex
edit: fixed typos in the original FIND
@peterjones Really appreciate it.
I mean it always should be after an A.
Could you please help me with it?
Alan Kilborn last edited by
PeterJones last edited by
it always should be after an A
Everything else stays the same
I just had to change the negative lookbehind that said “anything that doesn’t contain a digit” to be a positive lookbehind that said “only match if the character before is an
A1 B9 A12 B98 A123 B987 A1234 B9876 A12345 B98765
A0001 B9 A0012 B98 A0123 B987 A1234 B9876 A12345 B98765
So the B’s don’t change but the A’s do
Team A1 54
Team A36 63
Team A282 88
Team A1482 98
Team A0001 54
Team A0036 63
Team A0282 88
Team A1482 98
Where where zeros are added after letter A always if the numbers are less than 4.
@peterjones Thank you very much