Ask Your Question

need calc macro regex help

asked 2018-11-28 06:37:46 +0200

Todd2 gravatar image

updated 2018-11-28 06:44:42 +0200

Hi All,

I need to convert




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

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2018-11-28 06:43:45 +0200

updated 2018-11-28 09:50:44 +0200

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 service allows you to use regular expressions in search, but you need to do replacements manually.

edit flag offensive delete link more

answered 2018-11-28 06:54:27 +0200

Todd2 gravatar image

Not really an answer, but you can build your own. From

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
    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)
        Result = Result + Mid(Source, StartPos, Len(Source))
      End If                ' Position <> 0
  End If 

  Replace = Result
End Function
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-11-28 06:37:46 +0200

Seen: 226 times

Last updated: Nov 28 '18