# Help with macro? for multiple search and replace strings (regex)

I've created a list of Find (regex) and replace (regex) for documents I need to prepare for publication. See example below.

Presently the list is in a odt document in the form of a table.

When I open a new document to be checked, I want to be able to push a button or a shortcut which uses the first string to find in my list, checks "regular expressions" pastes this first string into the "replace" slot and hits Find.

At this point I would like to check manually the various instances I need to find and replace and move on to the second string find and replace with the same behaviour.

Is there a way of doing this without manually copying and pasting from the list to the search window?

Thanks a lot!

Example of one of the search and replace strings: In this case, I want unwanted \xA0 \t or other spaces between words or punctuation|words to be replaced by \x20 spaces.

Search: ([,|.|:|;|(|)|[|]|!|\?|¿|--|–|'|‹‹|››|‘|„|»|«|"|“|”|<|—]|[[:alnum:]+])([\x9+|\xA+|\xA0+\|\t+]+)([,|.|:|;|(|)|[|]|!|\?|¿|-|–|'|‹‹|››|‘|„|»|«|"|“|”|<|—]|[[:alnum:]+])

Replace: $1$3

I am attaching the list I have so far. The FR, EN, DE, ES, IT refer to the language the search is relevant for and the searches will mostly need to be done in the order they are shown, so I would have to jump from one language to the next relevant one. Or I could also create a separate table for each language.

But, most Important, I need to be able to check what is searched before I proceed with any replacement (as you see sometimes there is an m in the replacement column, meaning that I would have to work on the text myself, for example).

C:\fakepath\Temporary list of regex per language.odt

edit retag close merge delete

Everything you describe seems to be doable with a macro. But i would like to suggest to start smallish and leave everything in one file. If that is fine, i will try to writeup a first draft.

( 2020-11-14 13:57:11 +0100 )edit

Thank you @igorlius, do you mean that I could copy the list of find and replace strings in the document I am to work in?

( 2020-11-14 14:03:46 +0100 )edit

Yes, lets create a seperate page which will hold the regex data for now. We can move that into a seperate file later.

Just making some notes here for things we'll need

1. Assign Shortcut to macro (ref. https://ask.libreoffice.org/en/questi...)
2. MsgBox Function To Simulate a Search and Replace Question (ref. https://help.libreoffice.org/6.2/he/t...)
3. Submodule Macro to Search the active Writer Document (excluding first page, which holds the Search and replace Table) (ref. oTextSearch = CreateUnoService("com.sun.star.util.TextSearch") )

Open Question:

How should it be determined if a searchelement has been processed?

1. remove it (after search) ?
2. mark it as processed ?

Removing it is easier, but to redo the search, the string would have to be reinserted. Marking it will requires a some logic to process the markings.

So, what would you prefere?

( 2020-11-14 14:18:06 +0100 )edit

Thanks a lot for all this info @igorlius. I did not realize how many steps were needed. I do not want to take too much of your time. To answer your open question, as a matter of fact, once the first search and replace strings have been called in the search box, I may have to use the search function several times (together with the replace function from time to time) before I can move on to the next one. So I would need to be able to use the search and replace strings as many times as necessary. Does this answer your question?

( 2020-11-14 14:51:04 +0100 )edit

It's not to difficult, just connecting the "hopefully right" components in a useful way.
Just like playing with LEGO bricks.

So from what i gather for a first version you would not need a processed control, than i'll leaving that part out for now.

I think that shoul be sufficient informaiton to begin with. I'll come back with a first version. Later.

( 2020-11-14 15:09:48 +0100 )edit

I have come across one more issue/question.

Q: Do you need to be able to reference (aka. reinsert) parts of matched regular expressoin, into the repacement string?

I looks a litte like that since in your Quesiton you use $1$3 which look like placeholders form the original match.

( 2020-11-14 15:53:19 +0100 )edit

Yes Igorlius, I will often have replacements to be made, sorry I missed this post. If you want I can share the temporary table I have compiled so far to give you an idea of the number (I have an average of 2 pages per language so far) and kind of search and replace I am looking for.

( 2020-11-16 21:40:03 +0100 )edit

I will take a look and see what i can do and come back if i have something which i think might be helpful to you.

In the meantime, you might want to have a look at @Lupp 's suggestion/answer, He has a lot more experience with LO than me and his input might be more appropiate.

( 2020-11-16 22:03:04 +0100 )edit

( 2020-11-16 22:18:00 +0100 )edit

Hi, meanwhile I am just copying and pasting from the odt with the table to the other document's advanced find and replace. Is there a simple macro to do just that? I tried recording a macro what I do, but it will not work. Any clues?

( 2020-11-21 14:11:55 +0100 )edit

Sort by » oldest newest most voted

Hello (again)

I tried and failed to write a macro, But on my way i found this extension.

https://extensions.libreoffice.org/en...

which has a "Batch Mode" which "kind of" covers your usecase. You can use it to manage your search and replace patterns and apply (transfer) them and the execute.

It requires a little more manual interaction than i (personally) would like for this task, but i'll let you be the judge of that.

I checked that it load and seesm to work with my LO 7.1.0 .

Hope it helps.

PS: If you need help installing it, just ask in the comments.

more

Thank you @igorlius! I actually have alternative find and replace. I looked into batch mode and the only way to see if if works (i.e. allows to perform a search at a time) is to try it out. It looks as if one needs to enter a path to an editor (where I suppose the lines of the searches are stored). Fot linux they say kwrite, for windows notepad. Having a mac, kate seems to be the heir of kwrite, so I installed that, then entered the path /Applications/kate but it is not accepted by altsearch...

( 2020-11-22 12:04:27 +0100 )edit

The Editor option does not seem to work for me either, or i am doing something wrong, Even directly editing /home/<user>/.config/libreoffice/7.1.0./user/config/AltSearchEditor.txt does not seem to work.

But, it does not seems to be necessary. You directly edit

/home/<user>/.config/libreoffice/7.1.0./user/config/AltSearchScript.txt


which seems to contain all "Batches".

You don't even need to use the Refresh Button (in the UI).
The Transfer Button seems to direclty load the latest data from the file (when it is wirtten) which is nice.

So you can copy-convert you replacement table into AltSearchScript.txt and everything should kind of work.

Hope that this at least imporves your situation / work / task a little.

( 2020-11-22 15:10:32 +0100 )edit

Excellent suggestion @igorlius! Many thanks! I tried to put it in practice adding a line before the existing batches with one of my replacements. Unfortunately now the altsearch does not start any more, even now that I took these instructions out (error message below):

[Name] All [Sel.] Any non x20 (\x9+|\xA+|\xA0+\|\t+) between alpha with 1 x20
; the same parameters for all [Parameters] MsgOff Regular CurrSelection

Find([\x9+|\xA+|\xA0+\|\t+]+)([:alnum:]+) [Replace]$1$3 [Command] Replace

I can reinstall the altsearch, but there must be something wrong in my search and replace parameters. As you see I chose "Replace" as command instead of Replaceall to be able to do one replacement at a time. It would be even better if it did not move to the next but showed me what the replacement looks like first.

BASIC runtime error. An exception occurred Type: com.sun.star.resource ...(more)

( 2020-11-22 16:23:57 +0100 )edit

Not sure if that is the issue but the AltSearch Help, says, to use

\1 \2 \3 ... \9

when referencing subexpressions

Quote: If the search expression was searched for using parentheses (), \1 represents the contents of the first pair of parentheses, \2 corresponds to the contents of the second pair (), etc., up to \9, which corresponds to contents of the 9th pair. Only 1st level parentheses are valid; nested levels inside them are ignored.

( 2020-11-22 21:06:00 +0100 )edit

Thank you @igorlius, I'm back after some days spent on another part of the project.

I've started adapting my list to AltSearch (\$->\ for example), but something strange happens:

I am entering my data as follows:

[Name] ALL SEARCH Full stops [All] [Find]([:alpha:])(\s*)(.{2,}|…) [Parameters] MsgOff Regular [Replace]\1… [Parameters] MsgOff Regular [Command] ReplaceAll

To avoid not being able to see the single replacement (I'd be happy to avoid this extra step but I have not figure yet how) I hit the "Transfer" button to search one item at a time and then decide what to do with it.

And here comes the brunt.

The above expressions can be found with the find button and then I can either keep searching or replace the single instance with "Current selection" on to see the result.

But with the following expression (and others):

([—|–]|-{2,})(\s)(.?)(\s*)([—|–]|-{2 ...(more)

( 2020-12-01 12:05:55 +0100 )edit

Also, I tried modifying the AltSearchEditor.ini to make it open TextEdit (I also installed Kate in the meanwhile) in Mac to modify the AltSearchScript.txt file without opening it separately, but there must be something wrong with the settings I entered.

\applications\TextEdit.app ; kwrite ; c:\Program Files\PSpad editor\PSpad ; Uplna cesta k editoru textu pro upravu ulozenych skriptu ; vyhodnocuje se jen jen 1. radek ; ; Full path to text editor for script editing. ; Only first row is valid

Do you have any ideas?

( 2020-12-01 12:08:54 +0100 )edit

I did not yet study the already posted suggestions. Apologies!

Anyway I would suggest to do it the other way round.
I took the opportunity to develop (partly Q&D) a piece of software for the task which comes as a spreadsheet with included user code (Basic) usable
-- for the management of lots of F&R descriptions which may be selected for application one by one
-- for creating lists (sets) of files probably needing to get processed
-- for defining where the processed files are to store.

Download the attached document. Check the contained code to be sure it's not dangerous.

Then store the attached sheet and your testfiles (.odt) to the same otherwise empty folder, and create a subfolder for the output of processed files if wanted.

Enter into the first colored sheet the F&R descriptions you want to test, and the names of your testfiles following the examples and the explanations. Run the relevant Sub processFindAndReplaceInBatchMode.

If you don't get the expected results immediately, look for flaws in the data you entered. Be assured that I tested the solution successfully.

C:\fakepath\findandreplaceBatchProcessingV0_0_1a.ods

more

Thank you Lupp,

does the procedure you suggest allow to perform the find and replace manually?

The kind of work I need to do needs to be checked humanly at every search and replace since in most cases there will not be any replace at all untill a real case is found, so the process cannot be automatic.

( 2020-11-16 21:37:48 +0100 )edit

I don't know a way to open the usual F&R dialog with preset strings and options from a macro.
It is rather simple to "simulate" this per single replacement after prompting the user for the decision, but concerning cases where RegEx need to be used, I can only do it for a LibO version (6.2 or higher) already having the REGEX() function implemented. (Spreadsheet functions are available in LibO independent of the kind of document they are needed for.) My respective Q&D test was successful.

However, this is not just helping another user, but (kind of) developing software, and I wouldn't post the experimental code now. Let's see when I will find the time to get a releasable version, though v0.0.x, of course.

( 2020-11-17 11:19:58 +0100 )edit