Conversation

mephistophele-s

No description provided.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anastasiaSTR,

  1. Please check your Match sample. [0-9] will match only single digits, maybe it should be [0-9]+ to match numbers.
  2. Check you Split sample. If original string is Th//is-is-st, splitter is -, stop is //. The string will be splitted in [th, is, is, st] which is wrong.

let testString = "one 1 two 2 three 3"

let pattern = "[0-9]" // Matching numbers
let nsString = testString as NSString // Cating to NSString to use special methods

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I supposed "Cating" should be "Casting". And maybe not "special" but "specific".

var replaceRegExp: NSRegularExpression?

do {
replaceRegExp = try NSRegularExpression(pattern: "World", options: .allowCommentsAndWhitespace)
Copy link

@GreatAndPowerfulKing GreatAndPowerfulKing Jun 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are not stopping an execution on NSRegularExpression initialization failure, you may replace replaceRegExp declaration with: let replaceRegExp = try? NSRegularExpression(pattern: "World", options: .allowCommentsAndWhitespace).

@Gagnant

@anastasiaSTR, check this implementation. Feel free to ask me some questions.

extension String {
	
	private var range: NSRange {
		return NSMakeRange(0, self.utf16.count)
	}
	
	private var stringBegin: NSRange {
		return NSMakeRange(0, 0)
	}
	
	private var stringEnd: NSRange {
		return NSMakeRange(utf16.count, 0)
	}
	
	public func components(separatedBy regex: NSRegularExpression) -> [String] {
		let matchedRanges = regex.matches(in: self, range: self.range).map {
			return $0.range
		}
		return zip(matchedRanges + [stringEnd], [stringBegin] + matchedRanges).flatMap { next, current -> String? in
			let start = String.UTF16Index(current.location + current.length)
			let end = String.UTF16Index(next.location)
			return String(utf16[start ..< end])
		}.flatMap {
			$0.isEmpty ? nil : $0
		}
	}
	
}

You may use it in following manner.

let targetString = "This-is-string-to-split-by-sp//ecial-character"

if let regex = try? NSRegularExpression(pattern: "-") {
	print(targetString.components(separatedBy: regex))
}


// Trying to initialize regex
do {
let regex = try NSRegularExpression(pattern: pattern, options: [])
Copy link

@Gagnant Gagnant Jun 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose you to omit options parameter because it has default value []

// Trying to initialize regex
do {
let regex = try NSRegularExpression(pattern: pattern, options: [])
let results = regex.matches(in: testString, options: [], range: NSMakeRange(0, testString.characters.count)) // Matches

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must use testString.utf16.count here. Because NSRegularExpression works with NSString, which are utf16 encoded.

Sign up for free to join this conversation on . Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.