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"
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
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.)
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.
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".
It looks as if the pattern may be a little less regular than than that, or in need of slightly fuller description and definition:
The separators seem to include line endings, as in 900" OR "(1915)
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 ?
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.
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.