Advertisement
Larme

Untitled

Aug 11th, 2021
1,332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.91 KB | None | 0 0
  1. func regexUpdate() {
  2.     struct Item {
  3.         let source: String
  4.         let target: String
  5.     }
  6.     let values = [Item(source:"ab123451", target: "ab12345"),
  7.                   Item(source:"abc12345", target: "ab12345"),
  8.                   Item(source:"abc12", target: "ab12"),
  9.                   Item(source:"a12", target: "a")]
  10.     let pattern = "([a-zA-z]{1,2})[a-zA-Z]*([1-9]{1,5})[1-9]*"
  11.     values.forEach { aValue in
  12.         let modified = aValue.source.replacingOccurrences(of: pattern, with: "$1$2", options: .regularExpression)
  13.         print("Initial: \(aValue.source) - (modified) \(modified) \(modified == aValue.target ? "==" : "!=") \(aValue.target) (target) ")
  14.     }
  15. }
  16.  
  17.  
  18. Output:
  19.  
  20. Initial: ab123451 - (modified) ab12345 == ab12345 (target)
  21. Initial: abc12345 - (modified) ab12345 == ab12345 (target)
  22. Initial: abc12 - (modified) ab12 == ab12 (target)
  23. Initial: a12 - (modified) a12 != a (target)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement