Macro: find multiple terms and replace for a single one. Regex

Hi,

I’m trying do create a macro to “find and replace”, where in the “find” side there are several regex expressions; and in the “replace” side there is only one word. The following macro works for a single regex, but I don’t know how to put multiple regex in the line “oReplace.SearchString =”

Sub ReplaceFormatting
  'original code : Alex Savitsky
  'modified by : Laurent Godard
  

  Dim oDoc As Object
  Dim oReplace As Object
  Dim SrchAttributes(0) As New com.sun.star.beans.PropertyValue
  Dim ReplAttributes(0) As New com.sun.star.beans.PropertyValue

  oDoc = ThisComponent
  oReplace = oDoc.createReplaceDescriptor

  'Regular expression. Match any text
  oReplace.SearchString = "^\s*A"
  'Note the & places the found text back
  oReplace.ReplaceString = "<&>"
  oReplace.SearchRegularExpression=True  'Use regular expressions
  oReplace.searchStyles=False            
  oReplace.searchAll=True                'Do the entire document

 
  REM Now do the work!
  oDoc.replaceAll(oReplace)
End Sub

I tried things like this, with no success (gives me a syntax error message):

  • oReplace.SearchString = “^\sA", "^\s\d”
  • oReplace.SearchString = “^\sA" OR "^\s\d”
  • oReplace.SearchString = ("^\sA", "^\s\d")
  • oReplace.SearchString = Array("^\sA", "^\s\d")

Is it possible? I’m a layman, so please explain in simple terms.

Thanks,

In RegEx the comma in most places and the 2-letter-word OR always simply are literals without special functionality.
For a list of alternatives the list separator is the ‘Pipe’ symbol |.
For the “layman”: You cannot use Regex successfully without studying them to some depth. There are lots of tutorials in the web. Specifically good information, though probably a bit too complete for a beginner you find here: Regex Tutorial - Literal Characters and Special Characters .
Concerning RegEx usage in LibO specifically, you only need to know that the “flavor” is ICU.
See Regular Expressions - Old location of the ICU User Guide

It worked, thanks

Answer: make one giant regex

Thanks Lupp in the comments.

You should try to avoid the “giant RegEx” by finding common parts and few patterns describing the same set of accepted strings. In many realistic cases a list can be shortened with the help of patterns.
Concerning the original example ^\s(A|\d) would do.

I understand. Thanks for the explanation.