Page 1 of 1
Find & Replace help needed...
Posted: Mon Sep 10, 2012 10:51 am
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
Posted: Mon Sep 10, 2012 11:54 am
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
Posted: Mon Sep 10, 2012 12:44 pm
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.
Posted: Mon Sep 10, 2012 2:37 pm
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!
Posted: Mon Sep 10, 2012 3:45 pm
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.
Posted: Mon Sep 10, 2012 3:48 pm
by repeater
I think I'm with you. I've got some reading to do...!
Cheers for the help.
Steve
Posted: Mon Sep 10, 2012 4:01 pm
by ben_josephs
It would have been my pleasure to respond to someone with such good manners. But ak47wong got in there first.

Posted: Mon Sep 10, 2012 10:01 pm
by ak47wong
I suppose, then, the pleasure is all mine
