Extracting text and cycling through multiple instances

Hi there,

I am wanting to create a macro that can cycle through a list of case names that are separated by an 'x'. I want each extracted case name to be surrounded by " " with the word OR in-between and each case name placed back on to a clipboard ready for pasting. For example, I would like the macro to convert the following text:

[1915] 1 Ch 881 x [1914-15] All ER Rep 900
(1915) 84 LJ Ch 688 x (1915) 113 LT 159 x

to

"[1915] 1 Ch 881" OR "[1914-15] All ER Rep 900" OR "(1915) 84 LJ Ch 688" OR "(1915) 113 LT 159"

this is what I've got so far...


extract case names and surround with " ".kmmacros (3.2 KB)

I can see I am doing something wrong because the result at the end of the macro is empty, but I'm not sure where I am going wrong.

Any help or advice would be much appreciated.

Hi!
Am i thinking this all wrong or could this operation also be done with a single search and replace?

Separate Cases.kmmacros (16 KB)

EDIT: Or see now that I was slightly too quick, my suggestion does not work perfectly where the list of cases ends with a x-delimiter.
EDIT2: Had to run out now, so weren’t able to create/upload a macro that also works in the case of trailing x’s. But it could for instance be tackled by first searching the clipboard removing the trailing x’s, e.g. by a regex searching for \sx$ replacing with nothing

1 Like

With a couple of very minor changes to my macro that I posted a few weeks ago, (I was planning to include those changes in v1.1 of my macro) you can do everything with a single call to my macro, as follows. (I'm assuming you placed your data into a variable called LocalUserData, but you can use any variable you want.)

image

Basically it lets you specify the string " X ", (I just realized you wanted lowercase, which is easy to change) then splits every occurrence of " X " with a newline, then puts quotes around every line, then merges all lines, combining them with " OR ".

I'm not expecting you to download my filter macro and use it. I'm just showing that there are other interesting ways to solve your problem.

1 Like

So how would you do this manually?

  1. Replace every occurrence of %Space%x%Space% with "%Space%OR%Space%"
    • Going by your example text, the same needs to be done for any line break -- we'll assume optional spaces around that
  2. Remove any trailing x or " OR " (and associated white-space)
  3. Prepend and append "

This demonstrates on a variable, you can easily change it to work on the clipboard:

Text Extract.kmmacros (4.6 KB)

Image

You can combine some of those actions, but doing them separately makes it easy to see (and troubleshoot!) what's happening. Once it's all working you can consolidate -- as @Alexander says, you can probably get this down to one "trim" and one "search and replace".

1 Like

case names that are separated by an 'x'

It looks as if the pattern may be a little less regular than than that, or in need of slightly fuller description and definition:

  1. The separators seem to include line endings, as in 900" OR "(1915)
  2. you presumably need to avoid any lexical x that pops up in tokens like "Anaxerxes" or "Aix en Provence", so only "x" in a special environment for "x" will delimit, but that environment looks variable – sometimes a space to left and right, sometimes a bracket (square or round) to the right.

Until the pattern is a bit more fully and clearly defined, I think you may find that you get repeatedly tripped up and annoyed by a peppering of unanticipated edge-cases.


I wonder if that two-line example really provides a large enough sample to pin down the full range of cases that need to be dealt with ?

1 Like

That's great, thanks @Airy - I missed your original post. I think I will have a lot of use for something like your macro. It looks like it would save me from creating a lot bespoke solutions.

Thanks, @Alexander, @Airy, @Nige_S and @ComplexPoint for your help. @Nige_S's solution worked a treat! I have a better understanding of when not to use 'For each' now.

You are welcome. You are the first person to give me any feedback on that macro. I'm surprised I haven't got any feedback on it since I uploaded it 3 weeks ago. Maybe nobody needs text processing macros, but I think it's more likely a case of nobody knowing that they need it.