Home > Enterprise >  Removing wikitext hyperlinks in Swift
Removing wikitext hyperlinks in Swift

Time:08-04

I'm trying to remove wikitext hyperlinks from a MediaWiki API call, but everything I've tried either deletes nothing or deletes much more than expected.

I'm trying to check if the text between [[ and ]] contain a |, and if so remove the text between [[ and |, and also 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.

CodePudding user response:

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

CodePudding user response:

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: "")
}
  • Related