Replacing text with URL link

I need to search text in LibreOffice Writer using known criteria, find text excerpts and replace them with active URL links that contain found text. If found excerpt already is a link it should be skipped.


If I find TA0GED in text I have to replace it with active link in a manner that TA0GED is still displayed but converted to active link that leads to

I made regular expression that finds text excerpts as I need them.


It successfully finds everything. My problem is I have no idea what to do next. I manged to replace it with another string that contains found string but I cannot find proper way to convert it to a clickable link.

Internet search showed nothing on this topic.

Is that possible?

If your document is plain text with little formatting, you can convert it to HTML, where inserting hyperlinks by replacing text is easy - you just add the necessary tags. In Writer, you cannot just add the link tags - you would have to insert the xml code, but you can’t do that in a replace operation, as far as I know.

Besides writing a macro, you could also try using FODT, which is a plain XML, and do the replacements there using a plain text editor.

By the way, your regex T[EP][:digit:]+[:alpha:]{1,4} does not match your sample TA0GED :wink:

Along Andrew’s “useful Informations” with a bit from macro recording:

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

Sub makeLinksInSelection(Optional pUrlMain As String, Optional pRegEx As String)
If IsMissing(pUrlMain) Then pUrlMain = ""  'As an example'
If IsMissing(pRegEx)   Then pRegEx   = "T[EP][:digit:]+[:alpha:]{1,4}" 'As an example'
theSel  = ThisComponent.CurrentSelection
theText = ThisComponent.Text
sD      = ThisComponent.CreateSearchDescriptor
sD.SearchString            = pRegEx
sD.SearchRegularExpression = True

    'Adapted from a recorded Sub'
    slotMachine = ThisComponent.CurrentController.Frame
    dispatcher  = createUnoService("")
    dim args1(4) as new
    args1(2).Name = "Hyperlink.Target"
    args1(2).Value = ""
    args1(3).Name = "Hyperlink.Name"
    args1(3).Value = ""
    args1(4).Name = "Hyperlink.Type"
    args1(4).Value = 1
For j = 0 To theSel.Count - 1
 searchRg    = theSel(j)
 searchNext  = searchRg.Start
 searchEnd   = searchRg.End
  searchNext  = ThisComponent.FindNext(searchNext, sD)
  If IsNull(searchNext) Then Exit Do
  If ThisComponent.Text.CompareRegionEnds(searchNext, searchEnd)=-1 Then Exit Do
      args1(0).Name = "Hyperlink.Text"
      args1(0).Value = searchNext.String
      args1(1).Name = "Hyperlink.URL"
      args1(1).Value = pUrlMain & searchNext.String
      dispatcher.executeDispatch(slotMachine, ".uno:SetHyperlink", "", 0, args1()) 
Next j
End Sub


You can get the result by a simple search-replace by installing the AltSearch extension.

In the following example, replace all questions numbers with a link to this site:

  • Search for: [0-9]{6}
  • Replace: \h{}
  • Tick Regular expressions

You can click the Replace list to get help.

You must of course adapt to your needs :slight_smile:

HTH - Regards