Replace Character with 5 numbers

A place to try and solve your RegEx problems.
Post Reply
fraXis
Posts: 1
Joined: Mon Jul 06, 2015 7:14 pm

Replace Character with 5 numbers

Post by fraXis »

Hello,

I am using the trial of TextCrawler3 Pro and I am having trouble.

I want to replace all instances of "O12345" in a file to "N12345". The "12345" can be be any set of numbers so I set my regex to this:

[oO]\d\d\d\d\d


This works fine and detects all instances of O with 5 numbers after it.

But what do I set the replace string to? For example, I want to change "O93212" to "N93212", but for the life of me I can not figure out what to do in the replace box.

I tried N\d\d\d\d\d but that just changes the O93212 to N\d\d\d\d\d in the file.

Thanks.
silentguy
Posts: 6
Joined: Fri Dec 12, 2014 10:21 am

Re: Replace Character with 5 numbers

Post by silentguy »

Hi!
I can think of two ways to do it. I'll describe both, just for educational purposes :-D

1. Matching groups:
You can replace

[oO](\d\d\d\d\d)

with

N$1

Here $1 refers to the first expression in brackets...

2. Lookahead
Replace
[oO](?=\d\d\d\d\d)

with

N

This one tells it to find [oO] which is followed by five digits, but the important thing is that it just tells it to look if the five digits are there and not actually include them in the text to be replaced... And as a side note \d\d\d\d\d can also be written as \d{5}

Oh, and be aware that this one also replaces O1234567890 with N1234567890 (because the beginning of that matches your rule). If that is unwanted you could add a \b after the last digit, indicating that you are only interested in cases where there is a word boundary after the fifth digit.
Post Reply