Finding and replacing characters
-
Hi everyone,
I am struggling to find a way to replace characters for sorting purpose.
for example, I want to replace the following characters:
A36
A282
A1482into:
A0036
A0282
A1482where 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.
-
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).FIND =
(?<!\d)\d(\d)?(\d)?(\d)?(?!\d)
REPLACE =(?1:0)(?2:0)(?3:0)$0
SEARCH MODE = Regular ExpressionThis converted
A1 A36 A282 A1482
into
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
0
at the beginning instead” 3 times, for the three optional digits – so it will insert no0
s before a four-digit number, one0
before a three-digit number, two0
before a two-digit number, and three0
before 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.
----
Useful References
- 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?
-
@2-mins-summary said in Finding and replacing characters:
it always should be after an A
A quick mod to Peter’s for this would be to start off his Find expression with
A\K
-
it always should be after an A
FIND =
(?<=A)\d(\d)?(\d)?(\d)?(?!\d)
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
A
”A1 B9 A12 B98 A123 B987 A1234 B9876 A12345 B98765
becomes
A0001 B9 A0012 B98 A0123 B987 A1234 B9876 A12345 B98765
So the B’s don’t change but the A’s do
-
for example:
Team A1 54
Team A36 63
Team A282 88
Team A1482 98into:
Team A0001 54
Team A0036 63
Team A0282 88
Team A1482 98Where where zeros are added after letter A always if the numbers are less than 4.
Thank you
-
@peterjones Thank you very much