need calc macro regex help

Hi All,

I need to convert

"file:///K:\rn\abc\Spreadsheet\proposed4.png"

to

“file:///K:/rn/abc/Spreadsheet/proposed4.png”

This is very easy in Perl6

s:global| '\' |'/'|

SUBSITUTE gives me a run time error

args3(0).Value = SUBSTITUTE( args3(0).Value, "\", "/" )

How do I do a regex in LO Basic?

Many thanks,
-T

Well, in this specific case, I’d advise to better use ConvertToURL function instead of trying to replace parts of the string (with subsequent possible problems when the path would contain spaces and/or non-English characters).

Generally, possibly easiest is to use

Function Replace(str As String, strFind As String, strReplace As String) As String
  Replace = Join(Split(str, strFind), strReplace)
End Function

… which is, as turned out, already implemented as ReplaceString function of Tools standard library.

For using regular expressions in Basic, see here. Using com.sun.star.util.TextSearch service allows you to use regular expressions in search, but you need to do replacements manually.

Not really an answer, but you can build your own. From
https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/Strings_(Runtime_Library)

Public Function Replace(Source As String, Search As String, NewPart As String)
  Dim Result As String
  Dim StartPos As Long
  Dim CurrentPos As Long
 
  Result = ""
  StartPos = 1
  CurrentPos = 1
 
  If Search = "" Then
    Result = Source
  Else 
    Do While CurrentPos <> 0
      CurrentPos = InStr(StartPos, Source, Search)
      If CurrentPos <> 0 Then
        Result = Result + Mid(Source, StartPos, _
        CurrentPos - StartPos)
        Result = Result + NewPart
        StartPos = CurrentPos + Len(Search)
      Else
        Result = Result + Mid(Source, StartPos, Len(Source))
      End If                ' Position <> 0
    Loop 
  End If 
 
  Replace = Result
End Function