Search System Clipboard and Create Variable Values using Group of Regular Expressions

Hello,
I have a workflow that I am trying to automate using KM.
I'm trying to copy a question and answer choice, then paste its parts into its respective Chrome browser fields.
I planned as follows:

A: Copy text to system clipboard.
B. Use the Search using Regular Expression action with grouping to isolate six parts of system clipboard.

  1. RegEx 1: Text of question: ^1?[0-9]?[0-9]. (.+)$
  2. RegEx 2: Text of answer choice a: ^a. (.+)$
  3. RegEx 3: Text of answer choice b: ^b. (.+)$
  4. RegEx 4: Text of answer choice c: ^c. (.+)$
  5. RegEx 5: Text of answer choice d: ^d. (.+)$
  6. RegEx 6: Answer letter. A, B, C, D, T, or F: ^Ans: ([ABCDTF])$
    C. Paste the parts of the question and answers to the respective fields.
    D. Use the RegEx 6 variable to select the appropriate Chrome browser answer radio button.
    E. Click Submit.
    F. I would then go to the next question/answer set and run the KM Marco again.

Here are three random example items to be copied:

1. Lorem ipsum dolor “sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt” duis aute ______.
a. *nulla pariatur*
b. *duis aute*
c. occaecat
d. minim veniam
Ans: B

23. Purus faucibus ornare suspendisse sed nisi lacus sed.
Ans: T

36. ______ cras sed felis eget. Diam vulputate ut pharetra sit amet.
a. Suspendisse
b. Volutpat
c. Sagittis
d. Morbi
Ans: B

So far, I'm stuck at the Search using Regular Expression action. I would be grateful for any help.

Thanks
Copy-Paste Worksheet.kmmacros (10.2 KB)

I can see your problem, so here's my input. Your first regex is a bit off, even though it seems to work so I've corrected it. I've created the correct forms for the Search using Regular Expression actions. On the whole you weren't all that far off. Have a look:

I've put the third of your Q&A into the system clipboard so you can see what happens in KM.

By the way, the "^" only applies to the start of the text you are searching through, NOT to the start of each line which is why I've removed it.

Hope that helps.

1 Like

tiffle,
Wow. Thanks very much. Your Regular Expressions are simple and KM sees them just fine. And now that KM will assign values for the variables I can move forward with my project. I am look forward to learning more about Regular Expressions and all that they can do, especially in Keyboard Maestro.

It does help tiffle and I am grateful for your input.

1 Like

I enjoy a good challenge, especially a RegEx challenge, and this problem presents me with that opportunity.

Below is a macro that provides a more generalized solution which can process multiple questions at once. I have a Comment Action where you can insert processing Actions for each Q&A.

It also:

  • Show How to Use Optional RegEx Capture Groups
  • Show How to Use KM Dynamic Variables

Example Source String

1. Question with 4 choices
a. one a
b. one b
c. one c
d. one d
Ans: B

4. True-False Question with no choices
Ans: T

2. Question with 3 choices
a. two a
b. two b
c. two c
Ans: C

3. Question with 2 choices
a. three a
b. three b
Ans: A

RegEx:
(?mi)(\d+)\.\h+(.+)(?:\R[a-e]\.\h+(.+))?(?:\R[a-e]\.\h+(.+))?(?:\R[a-e]\.\h+(.+))?(?:\R[a-e]\.\h+(.+))?(?:\R[a-e]\.\h+(.+))?\RAns:\h+(.+)

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

Example Output

(note I revised the Q&As to make it easy to confirm results)

image

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:   Extract Data from Multiple Choice and True-False Questions [Example]

-~~~ VER: 1.0    2020-09-20 ~~~
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:

Extract Data from Multiple Choice and True-False Questions [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Extract Data from Multiple Choice and True-False Questions
    • Show How to Use Optional RegEx Capture Groups
    • Show How to Use KM Dynamic Variables

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Replace the Data in the "SET Clipboard to Source String" with your data
    • OR replace it with a COPY Action to copy your selection
  3. Trigger this macro.

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. ??
      .
      Make These Changes to this Macro
  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magenta color)
    • Set Clipboard to Source Str for Testing
      • CHANGE the data to your data, OR Disable and Enable the above COPY Action.

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @RegEx @Strings @CaptureGroups @DynamicVariables

1 Like

You're welcome.

It's good that you have a project to work on as that's probably the best way to learn a new tool or technique. However, if you ever get stuck again - just ask!!

Thank you very much. Your responds is detailed and clear. I'm trying to simplify a tedious copy and paste process.
Copy a question, choices, and answer letter and use it to complete the corresponding Chrome fields on a Create/Edit Multiple Choice Question form in a LMS (Learning Management System) called Blackboard.

  1. ______ cras sed felis eget. Diam vulputate ut pharetra sit amet. Paste to question field in Chrome.
    a. Suspendisse Paste to 1st answer field without letter.
    b. Volutpat Paste to 2nd answer field without letter.
    c. Sagittis. Paste to 3rd answer field without letter.
    d. Morbi. Paste to 4th answer field without letter.
    Ans: B Use to select the corresponding radio button.

Have KM select a checkbox in Chrome.

Have KM select an always the same dropdown box item in Chrome.

Have KM use the answer letter A,B,C or D to select the corresponding answer radio button.

Have KM click Submit and Create Another button.

I would copy next question by hand then run macro again.

The LMS's True/False form looks somewhat different. It only needs the question field completed and the one of two radio buttons selected True radio button or the False radio button. Then select Submit and Create Another button. I would copy next True False question and then run macro again.

You're welcome.
I did not see a question in your remaining post.
I think I have provided all of the KM Variables where you can do what you outlined.
Why don't you see if you can work with either the macro that @tiffle or I provided to automate your workflow. For simple, clarification questions, you can post back here.
But for more complicated questions, it would be best to post a new topic.