iosswiftregexwikiwikitext

Removing wikitext hyperlinks in Swift


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.

Solution

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