How find and replace multiple values ?

For example. It work for Notepad++

  • Text = 1 456 22 11
  • Find: (1)|(2)
  • Replace: (?1one)(?2two)
  • Result = one 456 twotwo oneone

How can it in LibreOffice Writer ?

LibO generally is using ICU Regex. See Regular Expressions - Old location of the ICU User Guide. There may be some specifics.
However, I don’t think ICU supports assigned replacement for captured groups…

If you don’t shy back from programming a bit, you can probably use nested calls to the REGEX() function to achieve your goals, if the text ranges to apply the replacement on are found and selected in advance. The com.sun.star.sheet.FunctionAccess service is available independent of the document model.

See this example.

Hi @Lupp,
I have a similar question. I’d like to replace (oe)|(ae)|(OE)|(AE) with œ æ Œ Æ
I had a look at your macro. How would I have to change it to make it work?
Would it also work if the text found is not mentioned (the way $1 $2 work)?
Thanks a lot!

REM  *****  BASIC  *****

Sub rawTest()
REM the text ranges to work on must be seleczed in advance.
REM This will be done mostly by a F&R action with an appropriate
REM search strung and 'Find All'.
REM the this macro can be run.
fa = createUnoService("com.sun.star.sheet.FunctionAccess")
rgs = ThisComponent.CurrentSelection
n = rgs.Count -1
For k = 0 To n
  rg = rgs(k)
  h = fa.callFunction("REGEX", Array(rg.String, "1", "one", "g"))
  h = fa.callFunction("REGEX", Array(h        , "2", "two", "g"))
  rg.String = h
Next k
End Sub  

(Slightly edited by @Lupp for better readability)

@Earendil: As I understand it, your question is about RegEx, not about LibreOffice.

The way I showed, you can, of course run 4 calls to REGEX() to achieve the special reult. If you want to get a macro being more flexible, you need to rework and extend my example in more than one place. That’s general programming, mainly. Of course you can prompt the user for a SearchString, and what you accept as the users description of the wanted replacements you may define by your own syntax, and evaluate by your own code. Have a lot of fun! (Many hours probably.)

I persoally frequently user RegEx, but I’m not an expert for everything in the field. You may study in depth the great site Regular Expression Tutorial - Learn How to Use Regular Expressions, and probably find a way to choose a replacement depending on the contents of captured groups. You then would need to test if ICU and LibO support it…

If an effort might pay will depend on many aspects of the intended usage of the result.

Hi @Lupp, thank you, I am learning myself in this field and I was looking for something simpler.

Just today, working with AltFindandReplace I stumbled upon this help topic:

text1||text2||text3||… - multiple search and replace operations in one step:
Add the sign || to the end of the search and replace expressions to delimit the partial searches and replacements.
Search for: text1||text2||text3
Replace: neco1||neco2||neco3
This will search for text1 and will replace it with neco1, then continue the search for text2, replace it by neco2, etc.

Unfortunately, after finding text1 the regex replaces it with the whole

neco1||neco2||neco3

Any ideas why this is happening?

@Earendil, thank you. I just tried it[AltFindandReplace] now and it worked correctly. LO 7.1.3.2.

AFAIK, you cannot do that on a single step: you need to first search & replace the “1” and then the “2.” The problem here is that while you can easily search for “1” or “2” in a single step with 1|2 (or (1)|(2) to later call the matched text), LibO’s replace box does not support many regular expressions. For instance, you can call the groups matched using \1 and \2, but there is no way set a “condition” to insert one group or the other, as in your example.

Hi,

Has anyone since Dec.2020 used the option ? It is still (?) as Earendil said:

Search for: text1||text2||text3
Replace: neco1||neco2||neco3
This would (!) search for text1 and will replace it with neco1, then continue the search for text2, replace it by neco2, etc.

Unfortunately, after finding text1 the regex replaces it with the whole

neco1||neco2||neco3

(Oh, and I get the "com.count.star… error message when I try to replace by “&” (the found string).

Or, as RGB-es remarks, is it an error in the manual?