Is It Possible to Update Each Line in a Collection That Ends With Matching Regex?

Hi all,

I am trying to evaluate each line of transcribed text and perform one of two actions. I've tried to modify several examples and have tested simpler versions of what I am trying to accomplish, but I am encountering two different types of failures.


Here is a sample text I am starting with.

SpeakerBob  1:43  
These are some transcribed words from SpeakerBob.

SpeakerJim  10:39  
These are more transcribed words from SpeakerJim. These words could wrap to multiple lines.

Here is the text I need to end with.

SpeakerBob 1:43  
	- These are some transcribed words from SpeakerBob.

SpeakerJim 10:39  
	- These are more transcribed words from SpeakerJim. These words could wrap to multiple lines.

(Please note - the bullets above should be dashes)


What I've Tried
I am starting with a For Each Local_Line action, with an If Any Action contained therein. In the If Any Action I am trying to use the logic:

  • If a line ends with regex matching :[0-5][0-9], then do nothing...
  • Otherwise, prepend a tab and a dash

My Questions:

  • Is it possible to use regex to match in this way? Is there perhaps another approach that makes more sense?
  • I tried to just use the logic "If the Local_Line contains a :" and, instead of ALL of the lines being available in System_Clipboard, the only lines that would be included were those that DID match the logic.

Thanks all in advance for any guidance you can provide. It's greatly appreciated.

  • Brian

This may or may not help depending on exactly what your needs are.

Your explanation

These words could wrap to multiple lines.

Is a little vague. If text is described as "wrapping to multiple lines", in many contexts it means that that text is actually just one line that happens to be long so the text editor "wraps" it on the page. Alternatively, it might mean that there are in fact multiple lines.

If there are multiple lines, you have to decide if each one of these lines is going to be:
TAB - line 1
TAB - line 2
TAB - line3
etc.
or whether you want just
TAB - line 1
line 2
line3

If it in fact is just one long line, then the whole thing is moot.

Anyway, if I were faced with this problem as an "infrequent" task, I would just do it in a program like BBEdit which has a good Regex engine. I would do it in three steps.

  1. Mark all the lines that end in the numbers you have described.

The Regex to accomplish that is below. (FIND)

^(.*:[0-5][0-9])$

I would replace those lines with some "unused" character in your text. For this example, I will assume that "@" does not normally appear as the first character of a SpeakerPerson line. (REPLACE ALL)

@\1

At this point, all the Speaker lines will look like

@SpeakerJim 10:39

  1. Any line that does NOT start with @ will be changed to start with TAB dash. The Regex to accomplish this is below. FIND

^([^@].*)

Prepend what you want: REPLACE ALL

\t-\1

TAB-These are more transcribed words from SpeakerJim. These words could wrap to multiple lines

  1. Any line that does start with a @ is then returned to its original state. It starts with a @ FIND

^@(.*)

REPLACE ALL by getting rid of the "@"

\1

Anyway, the above includes the Regex that will accomplish the task. If you do not own BBEdit (which if you deal with text much you should) there is a free version of BBEdit which I think does include its Regex [Grep] facilities. In any case, you can "experiment" with that program which can be helpful whenever trying to understand Regex commands. And it may well meet your needs to just put the text that you want to modify into BBEdit and do those three steps.

If you want to do everything in KM, then knowledge of the Regex commands outlined above might be helpful.

Thank you for the detailed and thoughtful response!

And agreed, my explanation around line wrapping was vague. You are correct that it is just one line that is long, such that the text editor wraps it.

I will check out BBEdit as well as update my Macro with the Regex you've provided.

Thanks again!

  • Brian

Hey Brian,

The basics of this problem are pretty simple. (See the appended macro.)

If your speaker text is hard-wrapped and not soft-wrapped, I'd want to have some real-world examples to test with.

@rlivingston has given you some good ideas for brute-forcing a solution, and I very much support his endorsement of BBEdit. I've used it nearly every day since 1994.

-Chris


Prepend Speaker Text with Tab- v1.00.kmmacros (5.7 KB)

The great thing about RegEx is that there are almost always multiple solutions.

Here is mine, which will work with only one line to be revised, and with multiple lines.
This is a great example showing use of Lookarounds.

Search For:
(?m)^(?!Speaker)(\w)

Replace with:
\t- \1

For RegEx details, see regex101: build, test, and debug regex

Example Output

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Add String to Start of Non-Speaker Lines [Example]

-~~~ VER: 1.0    2020-12-05 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Add String to Start of Non-Speaker Lines [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Add String to Start of Non-Speaker Lines [Example]

NOTICE: This macro/script is just an Example

  • It is provided only for educational purposes, and may not be suitable for any specific purpose.
  • It has had very limited testing.
  • You need to test further before using in a production environment.
  • It does not have extensive error checking/handling.
  • It may not be complete. It is provided as an example to show you one approach to solving a problem.

REQUIRES:

  1. KM 8.0.2+
  • But it can be written in KM 7.3.1+
  • It is KM8 specific just because some of the Actions have changed to make things simpler, but equivalent Actions are available in KM 7.3.1.
    .
  1. macOS 10.11.6 (El Capitan)
  • KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees. :wink:

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. :wink:
      .
  • Assign a Trigger to this maro.
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • ENABLE this Macro.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    • ALL Actions that are shown in the magenta color