1

I'm trying to remove Wikitext hyperlink formatting and just get the link text, but everything I've tried either deletes nothing or deletes much more than expected. Here's the general pattern:

[[user interface]] → user interface
[[Telephone call|calls]] → calls
[[camera phone|take pictures]] → take pictures

If the text between [[ and ]] contains a |, I try to remove the text between [[ and |, remove the closing brackets, but retain the text in-between | and ]]. If it doesn't contain a |, I simply want to remove the brackets, retaining the text between them.

Example:

The iPhone has a [[user interface]] built around a [[multi-touch]] screen. It connects to [[cellular network]]s or [[Wi-Fi]], and can make [[Telephone call|calls]], [[web browser|browse the web]], [[camera phone|take pictures]], [[portable media player|play music]] and send and receive [[email]]s and [[text messaging|text messages]].

Expected result:

The iPhone has a user interface built around a multi-touch screen. It connects to cellular networks or Wi-Fi, and can make calls, browse the web, take pictures, play music and send and receive emails and text messages.

I discovered this answer but upon plugging it into my app, it removed all of the text between [[ and ]], instead of the text between [[ and |.

Using the example above, this is what the code from that answer results in:

The iPhone has a {removed} built around a {removed} screen. It connects to {removed}s or {removed}, and can make {removed}, {removed}, {removed}, {removed} and send and receive {removed}s and {removed}.

This is really leaving me stumped, can anyone help? Thanks!

extension String {
    func replacingOccurrencesHyperlinks() -> String {
        let regExpr = "\\[\\[[^\\]]+?\\|(.+?)\\]\\]"
        return replacingOccurrences(of: regExpr, with: "{removed}", options: .regularExpression)
    }
} // this is the extension I use on the MediaWiki API result.
aheze
  • 24,434
  • 8
  • 68
  • 125
David
  • 87
  • 12
  • You probably just need `"\\[\\[.*?]]"` – Wiktor Stribiżew Aug 03 '22 at 21:12
  • @WiktorStribiżew Thanks for replying, but unfortunately that didn't work. Upon plugging it in I get this result: https://imgur.com/a/4m80DsB, still deleting more text than it should. – David Aug 03 '22 at 21:17
  • What is wrong? What extra text is replaced? What exact rule is there in place? – Wiktor Stribiżew Aug 03 '22 at 21:20
  • As seen in my original post, I'm trying to achieve the result shown in the text underneath 'Expected result' in my post, but your code still spits out what the original one did, shown in both the screenshot I sent in the last comment and in the last quote of the original post. @WiktorStribiżew – David Aug 03 '22 at 21:24

2 Answers2

1

there is probably a better one liner regex solution, but I forgot how it works, in the meantime try this to print the Expected result:

func replacingOccurrencesHyperlinks() -> String {
    return self.replacingOccurrences(of: "\\[\\[(?:[^\\]|]*\\|)", with: "", options: .regularExpression)
    .replacingOccurrences(of: "[[", with: "")
    .replacingOccurrences(of: "]]", with: "")
}
0

Resolved by an awesome guy over on Reddit. Just needed to replace with: "{removed}" in the extension with simply with: "$1"!

David
  • 87
  • 12
  • that does not give the `Expected result` that you showed us, it leaves the brackets. – workingdog support Ukraine Aug 03 '22 at 22:22
  • I had to add another call to replacingOccurances to remove the brackets, but you get the idea. – David Aug 04 '22 at 07:30
  • yeah, I get the idea, just like my answer, it does all of that and is more compact. – workingdog support Ukraine Aug 04 '22 at 08:31
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 08 '22 at 17:12