Find & Replace help needed...

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
repeater
Posts: 3
Joined: Mon Sep 10, 2012 10:44 am

Find & Replace help needed...

Post by repeater »

Hi,

I'm not very good at this sort of thing and wondered if anyone could help me?

I am trying to do a find and replace on a string of data similar to the one below.

STX=ANA:1+BBB1:FREE TEXT+BBB1:17+120907:220025+MTAD005854 001+

In the string, the + sign is effectively a delimiter. So what I would like to find and replace is the data only between the 4th and the 5th + sign - in this case `MTAD005854 001`

The problem I have is that the only fixed information I have is that the lines I want to change will always begin STX= and the data to be changed is between the 4th & 5th + sign.

The rest of the information is different on each line and I need this to remain unchanged. Additionally, I would like to replace the located section with a 6 digit numerical string incremented by 1 each time.

Is this even possible or am I trying to do too much?

Thanks

Steve
ak47wong
Posts: 703
Joined: Tue Aug 12, 2003 9:37 am
Location: Sydney, Australia

Post by ak47wong »

It's possible, but a bit tedious. First, enable POSIX regular expression syntax in Configure > Preferences > Editor.

The easy part is replacing the data between the 4th and 5th plus sign with an incrementing number:

Find what: (STX=[^+]+\+[^+]+\+[^+]+\+[^+]+\+)[^+]+
Replace with: \1\i

Select Regular expression.
Click Replace All.

The problem is that the numbers aren't 6 digits in length, so you'll have to pad them with zeros in five separate steps:

Find what: (STX=[^+]+\+[^+]+\+[^+]+\+[^+]+\+)([0-9]{5}\+)
Replace with: \10\2

Find what: (STX=[^+]+\+[^+]+\+[^+]+\+[^+]+\+)([0-9]{4}\+)
Replace with: \100\2

Find what: (STX=[^+]+\+[^+]+\+[^+]+\+[^+]+\+)([0-9]{3}\+)
Replace with: \1000\2

Find what: (STX=[^+]+\+[^+]+\+[^+]+\+[^+]+\+)([0-9]{2}\+)
Replace with: \10000\2

Find what: (STX=[^+]+\+[^+]+\+[^+]+\+[^+]+\+)([0-9]\+)
Replace with: \100000\2
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

It's less tedious if you start the incrementing numbers at 1000001:
Find what: ^(STX=([^+]+\+){4})[^+]+
Replace with: \1\i(1000001)

[X] Regular expression

Replace All
You then just need to remove the leading 1.

In the search expression ^ anchors each match to the beginning of a line, and {4} indicates that the preceding expression matches 4 times.
repeater
Posts: 3
Joined: Mon Sep 10, 2012 10:44 am

Post by repeater »

Thanks Guys - I'm really grateful you took the time to help as you have definately solved my problem.

However, I have an inquisitive mind and I wanted to ask a question. In the replace string, how does it know where in the original text to replace the number? As far as I can see, the '1' seems to point to it - is there some sort of anchor in the find above that indicates where to put it?

I appreciate you are probably busy people so if you don't have time to respond I will simply go away happy that you solved my problem and try and look it up myself (Google is my friend)...

Thanks again!
ak47wong
Posts: 703
Joined: Tue Aug 12, 2003 9:37 am
Location: Sydney, Australia

Post by ak47wong »

repeater wrote:In the replace string, how does it know where in the original text to replace the number? As far as I can see, the '1' seems to point to it - is there some sort of anchor in the find above that indicates where to put it?
The parentheses in the search string denote a tagged expression. The numbered back-references (\1 and so on) refer to the contents of the tagged expressions from left to right. So, in the expression:

Find what: ^(STX=([^+]+\+){4})[^+]+

The back-reference \1 matches the part of the expression in red:

Find what: ^(STX=([^+]+\+){4})[^+]+

Have a look in the help under How To > Find and Replace Text > Use Regular Expressions and How To > Reference Information > Regular Expressions. Or read more here.
Last edited by ak47wong on Mon Sep 10, 2012 3:48 pm, edited 1 time in total.
repeater
Posts: 3
Joined: Mon Sep 10, 2012 10:44 am

Post by repeater »

I think I'm with you. I've got some reading to do...!

Cheers for the help.

Steve
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

It would have been my pleasure to respond to someone with such good manners. But ak47wong got in there first. :-)
ak47wong
Posts: 703
Joined: Tue Aug 12, 2003 9:37 am
Location: Sydney, Australia

Post by ak47wong »

I suppose, then, the pleasure is all mine :)
Post Reply