Regular expressions are anything but. Here are some ways to remember how to use them via example.
First, the NSRegularExpression class, and how it is used:
1 2 3 4 5 6 7 8 9 10 |
|
This code can be broken into the following parts:
1) + (NSRegularExpression )regularExpressionWithPattern:(NSString )pattern options:(NSRegularExpressionOptions)options error:(NSError **)error
Create a regular expression using NSRegularExpression. Consider options such as case insensitivity and continuation across lines (NSRegularExpressionDotMatchesLineSeparators). We did not record the error here, though you are certainly welcome to.
Our simple regex pattern here is “.”, which effectively means, find every individual character.
2) - (NSArray )matchesInString:(NSString )string options:(NSMatchingOptions)options range:(NSRange)range
This method returns an array of NSTextCheckingResults, or ranges in which matches were found.
3) Everything after this method
The rest of this code is simply meant to produce the expected result for the first match, which will be a capital “M”. What you do with the ranges once you have them is up to you. That’s the easy part, because you are no longer dealing with regular expressions.
So what are some regular expression operators you should know?
{n} - match something exactly n times {n, } - match at least n times (as many times as possible) {n, m} - match between n and m times, inclusive | - match either before or after the pipe
And what are some metacharacters you should know?
\d - Match any decimal digit in the Unicode set of characters \D - Match any non decimal digit \r - Match a carriage return \s - Match a whitespace character . - Match any character ^ - Match only if at the beginning of the line $ - Match only from the end of the line \ - Character escape for the following characters: * ? + [ { } ( ) ^ $ | \ . /
So some nice regular expressions to have in your back pocket (please note I have not validated these!):
1 2 3 4 5 6 7 |
|
Sources
2) http://www.raywenderlich.com/30288/nsregularexpression-tutorial-and-cheat-sheet
3) http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation