Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func regexUpdate() {
- struct Item {
- let source: String
- let target: String
- }
- let values = [Item(source:"ab123451", target: "ab12345"),
- Item(source:"abc12345", target: "ab12345"),
- Item(source:"abc12", target: "ab12"),
- Item(source:"a12", target: "a")]
- let pattern = "([a-zA-z]{1,2})[a-zA-Z]*([1-9]{1,5})[1-9]*"
- values.forEach { aValue in
- let modified = aValue.source.replacingOccurrences(of: pattern, with: "$1$2", options: .regularExpression)
- print("Initial: \(aValue.source) - (modified) \(modified) \(modified == aValue.target ? "==" : "!=") \(aValue.target) (target) ")
- }
- }
- Output:
- Initial: ab123451 - (modified) ab12345 == ab12345 (target)
- Initial: abc12345 - (modified) ab12345 == ab12345 (target)
- Initial: abc12 - (modified) ab12 == ab12 (target)
- Initial: a12 - (modified) a12 != a (target)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement