Ask Your Question
0

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

"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

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

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

edit flag offensive delete link more
0

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

Todd2 gravatar image

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
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 141 times

Last updated: Nov 28 '18