Restrict a Regex search & replace to certain records

44 Views Asked by At

I have batches of records to which I am applying standardizing formatting. Each batch has several header records which should not be touched, followed by hundreds of user records.

One such formatting is to add a full-stop after any initial or abbreviation which does not have one, such as "Smith, Dan F O'M," or "St Sebastian".

My (Perl based) Regex in Textpad starts like this-

(\<[[:upper:]]\>(?:'[[:upper:]])?)([", ])

which says find a "word" of 1 character, optionally followed by apostrophe and 1 character, then terminated with any of quote, comma or space. (ignoring other abbrev for the sake of clarity). The replace inserts the full-stop-

  $1.$2

My problem is to exclude the header rows. The header rows do not begin with alpha, and the user rows do. My thought is to add at the front-

 ^[[:alpha:]].*?

This works by selecting from the start of the record, but I then need to run the "replace all" multiple times for as many abbreviations as could reasonably occur in a record (three seems OK).

Is there any Regex construct to "home in" to the user records and only select/replace each small element, so the cursor does not move past the start of the next initial in the same record?

1

There are 1 best solutions below

0
On

Providing an update in answer to my question-

I obtained an answer on the Textpad Forum when I put this problem there. It appears there is no general construct to enable multiple matches and replaces in one pass, while excluding the rows which do not start with an alpha. This is because the cursor point will have moved past the replace point.

The forum moderator suggested the possibility of Regex conditional expressions to solve the problem. i.e. code a match expression for 1 initial, for 2 initials, for 3 initials. This could work if the format is well structured. However the data I have are too variable.

I will solve the need by the unsophisticated approach of executing the find and replace multiple times, as suggested in the problem text, and automated in a macro.

Lyle