NSRegularExpression in Swift
There are several considerations when implementing Regular Expressions in Swift.
In order to get an accurate range length that supports all character types the input string must be converted to a NSString.
For safety matching against a pattern should be enclosed in a do catch block to handle failure
Regular expression functionality is often put in an extension or helper to seperate concerns.
Extending String to do simple pattern matching
Below is another example which builds on the above to do something useful, which can't easily be done by any other method and lends itself well to a regex solution.
NSRegularExpression for mail validation
or you could use String extension like this:
Patterns can be used to replace part of an input string.
The example below replaces the cent symbol with the dollar symbol.
To match special characters Double Backslash should be used
\. becomes \\.
Characters you'll have to escape include
The below example get three kinds of opening brackets
Regular expressions can be used to validate inputs by counting the number of matches.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors
and released under CC BY-SA 3.0