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.

Example:

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 http://mysite.org/TA0GED.

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

T[EP][:digit:]+[:alpha:]{1,4}

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 = "https://yourdomain.com/heap/"  '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("com.sun.star.frame.DispatchHelper")
    dim args1(4) as new com.sun.star.beans.PropertyValue
    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
 Do
  searchNext  = ThisComponent.FindNext(searchNext, sD)
  If IsNull(searchNext) Then Exit Do
  If ThisComponent.Text.CompareRegionEnds(searchNext, searchEnd)=-1 Then Exit Do
  ThisComponent.CurrentController.Select(searchNext)
      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()) 
 Loop
Next j
ThisComponent.CurrentController.Select(theSel)
End Sub

Hi

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{https://ask.libreoffice.org/en/question/&}
  • Tick Regular expressions

You can click the Replace list to get help.

You must of course adapt to your needs :slight_smile:

HTH - Regards