0

I want to transpose all characters A with an accent to ASCII with swift's regular expression.

let pattern = "[äÄàÀ]"
var input = "ÀàÄä"
input.replacingOccurrences(of: pattern, with: "A", options: .regularExpression)

The following code returns in Xcode 9: AAAAAA. (6 times A). I would expect 4 times A. What is wrong?

  • 1
    [4 `A`s here](http://swift.sandbox.bluemix.net/#/repl/59c2e3e95b5779594c2f1169) – Wiktor Stribiżew Sep 20 '17 at 21:56
  • 1
    This might be helpful: https://stackoverflow.com/questions/29521951/how-to-remove-diacritics-from-a-string-in-swift – Martin R Sep 20 '17 at 22:00
  • What is the Xcode version that you are using? As already mentioned by Wiktor it returns AAAA with the latest Xcode AppStore release which is Xcode 9 version 9A235 – Leo Dabus Sep 20 '17 at 22:06
  • 1
    If you copy/pasted those characters from somewhere, make sure you don't have any invisible formatting characters in your pattern and input string. – CAustin Sep 20 '17 at 22:08
  • It returns AAAA for me with Xcode 9 and Swift 4/Swift 3.2 – Vini App Sep 20 '17 at 23:36
  • A simple example which generates 6 A's. `let pattern = "[a\u{308}A\u{308}\u{E0}\u{C0}]" //<- Seemingly "[äÄàÀ]"` `var input = "\u{C0}\u{E0}A\u{308}a\u{308}" //<- Seemingly "ÀàÄä"` `input.replacingOccurrences(of: pattern, with: "A", options: .regularExpression) //<- Generates "AAAAAA"`. – OOPer Sep 21 '17 at 11:06

0 Answers0