

Notice how each of those regexes are made using regex literals – the ability to create a regular expression by starting and ending your regex with a /. But what is regex A regular expression (shortened as regex or regexp also referred to as rational expression) is a sequence of characters that define a search pattern.

In that first regular expression we’re asking for the range of all substrings that match any lowercase alphabetic letter followed by “at”, so that would find the locations of “cat”, “sat”, and “mat”.In case you’re not familiar with regular expressions: Print(message.replacing(/at/, with: "dog")) the var mutable is defined as shorthand to convert an input string into an NSMutableString under the covers. Print(message.replacing("cat", with: "dog"))īut the real power of these is that they all accept regular expressions too: print(message.ranges(of: /at/)) Some regular expression operators for Swift. To see what’s changing, let’s start simple and work our way up.įirst, we can now draw on a whole bunch of new string methods, like so: let message = "the cat sat on the mat" Put together this is pretty revolutionary for strings in Swift, which have often been quite a sore point when compared to other languages and platforms. SE-0357 adds many new string processing algorithms based on regular expressions.SE-0354 adds the ability co create a regular expression using /./ rather than going through Regex and a string.

SE-0351 introduces a result builder-powered DSL for creating regular expressions.This is actually a whole chain of interlinked proposals, including Paul Hudson 5.7 introduces a whole raft of improvements relating to regular expressions (regexes), and in doing so dramatically improves the way we process strings.
