import Foundation
// WARNING: You included a flag that Swift doesn't support: u
// When this flag is set, it makes the pattern and subject strings to be treated as unicode.
// Swift already treats the pattern and subject strings as unicode by default, so including this flag is redundant.
let pattern = #"(?|(e)(pisodes?)|(c)(hapters?)|(c)(hs?)|(e)(ps?)|(c)(s?)|(e)(s?)|()())\s*((\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+-]|and)\s*(?:(?:e(?:pisodes?)|c(?:hapters?)|c(?:hs?)|e(?:ps?)|c(?:s?)|e(?:s?)|)\s*)?(?4))*)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let testString = #"""
chapter 2A Hello
chapter 2AHello
text chapter 25.6 text
// c~25.6
text chapters 23, 24, 25 text
// c~23-25
text chapter 23, 25 text
// c~23 & 25
text chapter 23 & 24 & 25 text
// c~23-25
text c25.5-30 text
// c~25.5-30
text c99-c102 text
// c~99-102
text chapter 1 - 3 text
// c~1-3
33 text chapter 1, 2 text 3
// c~1-2
chapter 25.6 & chapter 29
// c~25.6 & 29
chapter 25+c26
// c~25-26
chapter 25 + 26 + 27
// c~25-27
chapter 10.4C
= c~10.4 part 3
chapter 2B
= c~2 part 2
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #""#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression