Page 1 of 1

Multiple search/replace from start of each line

Posted: Tue Oct 20, 2020 4:47 am
by lyle.walker
I need to add full stops to initials in a series of records, but only for the records where the first character is an alpha. Other records not beginning with a name may also contain initials but these must not be touched.

so like example:
Smith, Robin John G H C, bla, bla...
Thompson, Ian P, bla, bla..


I can limit initial matches with the following regex:

Code: Select all

^([[:alpha:]].*?\<[[:alpha:]]\>)([, ])
and then replace with and added full-stop as follows:

Code: Select all

$1.$2
This finds the "G" above, but then due to the cursor positioning after match, it does not find the H and C in that same record.

Is there some way to achieve this, without repeating the replace all on the file multiple times?

I am using Textpad 8.2

thanks, Lyle

Posted: Tue Oct 20, 2020 9:55 am
by ben_josephs
If the maximum number of initials is small you might try matching up to that number of them and using conditional expressions in the replacement, but I seriously wouldn't recommend it.

Otherwise, there is no way I know of to do what you want in a single application of a repeated replacement because, as you say, once each replacement is made the cursor is moved to the end of what was matched, and the preceding context is lost.

Posted: Tue Oct 20, 2020 9:28 pm
by lyle.walker
Thank you Ben.

The data are too varied for the option of conditional expressions, as far as I could construct. Initials may not be all together in one spot, and include compound abbreviations.

I can repeat my replace-all say 3-4 times inside a macro, it may take a fraction longer to run.

But conditional expressions look quite interesting, and I must practice them for other situations.

Lyle

Posted: Wed Oct 21, 2020 2:44 pm
by AmigoJack
I'm surprised this works for you in the first place, neither distinguishing between upper and lowercase, nor disallowing non-letters. And not thinking about edge cases. It should also match the bold parts of these examples:
  • Match a word
  • Charles I, prince Kent III
  • Mary and I had fun
  • Mary & I walked
  • Could I, please?
  • CO2+N goes
  • ISTC: A02-2009-000004BE-A, ISBN: 978-3-16-148410-0
In other words: are you sure you have no false positives?