Common regular expressions for iOS development

Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p> <ul> <ul> <li>Ways to implement regular expression matching</li> <li>Common Regular Expressions</li> </ul> </ul><p><p></p></p>Ways to implement regular expression matching<p><p>There are three ways to match regular expressions in Ios.</p></p><p><p><strong>1. Using nspredicate (predicate) Matching</strong></p></p><p><p>For example, to match a valid mailbox:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs objectivec"><span class="hljs-built_in">NSString</span> *email = @<span class="hljs-string">"[email protected]"</span>;<span class="hljs-comment">// 邮箱的正则表达式</span><span class="hljs-built_in">NSString</span> *emailRegex = @<span class="hljs-string">"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"</span>;<span class="hljs-comment">// 创建并返回一个由 给定格式创建的新的字符串 形成的新谓词, 并解析结果.</span>NSPredicate *emailPredicate = [NSPredicate predicateWithFormat:@<span class="hljs-string">"SELF MATCHES %@"</span>, emailRegex];<span class="hljs-comment">// 返回布尔值, 给定对象是否与接受者的条件匹配</span><span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"%d"</span>, [emailPredicate evaluateWithObject:email]);</code></pre></pre><p><p>Use Nspredicate's string evaluation function for regular expression processing with the following keyword: MATCHES. predicate matching is more flexible, but you need to know the knowledge about Predicates.</p></p><p><p><strong>2. Using Rangeofstring:options: Direct Lookup</strong></p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs objectivec"><span class="hljs-built_in">NSString</span> *email = @<span class="hljs-string">"[email protected]"</span>;<span class="hljs-built_in">NSRange</span> range = [email rangeOfString:@<span class="hljs-string">"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"</span> options:NSRegularExpressionSearch];<span class="hljs-comment">// NSNotFound, 常量, 表明一项请求无法找到或者不存在的值。</span><span class="hljs-keyword">if</span> (range<span class="hljs-variable">.location</span><span class="hljs-built_in">NSNotFound</span>) { <span class="hljs-comment">// 在给定的范围内返回一个包含接受者特点的字符串对象</span> <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"%@"</span>, [email substringWithRange:range]);}</code></pre></pre><p><p>The options set in Nsregularexpressionsearch means that a regular expression match is used to return the position of the first matching Result.</p></p><p><p><strong>3. Match using nsregularexpression (regular expression Class)</strong></p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs applescript">NSString *email = @<span class="hljs-string">"[email protected]"</span>;NSRegularExpression *emailRegex = [NSRegularExpression regularExpressionWithPattern:@<span class="hljs-string">"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"</span><span class="hljs-keyword">error</span>:nil];NSTextCheckingResult *<span class="hljs-constant">result</span> = [emailRegex firstMatchInString:email options:<span class="hljs-number">0</span> range:NSMakeRange(<span class="hljs-number">0</span><span class="hljs-property">length</span>])];<span class="hljs-keyword">if</span> (<span class="hljs-constant">result</span>) { NSLog(@<span class="hljs-string">"%@"</span>, [email substringWithRange:<span class="hljs-constant">result</span>.range]);}</code></pre></pre><p><p>Using the System's Regular expression class (nsregularexpression) returns multiple results that Match.</p></p><p><p><strong><em>Summary:</em></strong></p></p><p><p>The first way to master the use of nspredicate, you need to consult the Apple API or related technical documentation, if only consider the first matching results, the second is simpler, if you need to match multiple results, and match several times, the third way is more Efficient.</p></p>Common Regular Expressions<pre class="prettyprint"><code class=" hljs objectivec"><span class="hljs-comment"><span class="hljs-comment">//verify Mailbox</span></span>+ (<span class="hljs-built_in"><span class="hljs-built_in">BOOL</span></span>) Verifyemail: (<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*) email{<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*emailregex = @<span class="hljs-string"><span class="hljs-string">"[a-z0-9a-z._%+-][email Protected][a-za-z0-9.-]+\\. [a-za-z] {2,4} "</span></span>; Nspredicate *emailpredicate = [nspredicate predicatewithformat:@<span class="hljs-string"><span class="hljs-string">"self MATCHES%@"</span></span>, emailregex];<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>[emailpredicate evaluatewithobject:email];}<span class="hljs-comment"><span class="hljs-comment">//verify mobile phone number</span></span>+ (<span class="hljs-built_in"><span class="hljs-built_in">BOOL</span></span>) Verifymobilephone: (<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*) mobilephone{<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*phoneregex = @<span class="hljs-string"><span class="hljs-string">"^ ((13[0-9)) | ( 15[^4,\\d]) | (18[0,0-9])) \\d{8}$ "</span></span>; Nspredicate *phonepredicate = [nspredicate predicatewithformat:@<span class="hljs-string"><span class="hljs-string">"self MATCHES%@"</span></span>, phoneregex];<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>[phonepredicate evaluatewithobject:mobilephone];}<span class="hljs-comment"><span class="hljs-comment">//verify User Name</span></span>+ (<span class="hljs-built_in"><span class="hljs-built_in">BOOL</span></span>) Verifyusername: (<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*) username{<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*usernameregex = @<span class="hljs-string"><span class="hljs-string">"^[a-za-z0-9]{6,20}+$"</span></span>; Nspredicate *usernamepredicate = [nspredicate predicatewithformat:@<span class="hljs-string"><span class="hljs-string">"self MATCHES%@"</span></span>, usernameregex];<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>[usernamepredicate evaluatewithobject:username];}<span class="hljs-comment"><span class="hljs-comment">//verify Password</span></span>+ (<span class="hljs-built_in"><span class="hljs-built_in">BOOL</span></span>) Verifypassword: (<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*) password{<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*passwordregex = @<span class="hljs-string"><span class="hljs-string">"^[a-za-z0-9]{6,20}+$"</span></span>; Nspredicate *passwordregexpredicate = [nspredicate predicatewithformat:@<span class="hljs-string"><span class="hljs-string">"self MATCHES%@"</span></span>, passwordregex];<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>[passwordpredicate evaluatewithobject:password];}<span class="hljs-comment"><span class="hljs-comment">//verify Nickname</span></span>+ (<span class="hljs-built_in"><span class="hljs-built_in">BOOL</span></span>) Verifynickname: (<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*) nickname{<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*nicknameregex = @<span class="hljs-string"><span class="hljs-string">"^[\u4e00-\u9fa5]{4,8}$"</span></span>; Nspredicate *nicknamepredicate = [nspredicate predicatewithformat:@<span class="hljs-string"><span class="hljs-string">"self MATCHES%@"</span></span>, nicknameregex];<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>[nicknamepredicate evaluatewithobject:nickname];}<span class="hljs-comment"><span class="hljs-comment">//verify ID Number</span></span>+ (<span class="hljs-built_in"><span class="hljs-built_in">BOOL</span></span>) Verifyidentitycard: (<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*) identitycard{<span class="hljs-built_in"><span class="hljs-built_in">BOOL</span></span>Flag<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(identitycard<span class="hljs-variable"><span class="hljs-variable">. Length</span></span><=<span class="hljs-number"><span class="hljs-number">0</span></span>) {flag =<span class="hljs-literal"><span class="hljs-literal">NO</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Flag }<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*idregex = @<span class="hljs-string"><span class="hljs-string">^ (\\d{14}|\\d{17}) (\\d|[ XX]) $ "</span></span>; Nspredicate *identitycardpredicate = [nspredicate predicatewithformat:@<span class="hljs-string"><span class="hljs-string">"self MATCHES%@"</span></span>, idregex];<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>[identitycardpredicate evaluatewithobject:identitycard];}<span class="hljs-comment"><span class="hljs-comment">//verify Letters and numbers</span></span>+ (<span class="hljs-built_in"><span class="hljs-built_in">BOOL</span></span>) Verifyfigureandletter: (<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*) string{<span class="hljs-built_in"><span class="hljs-built_in">NSString</span></span>*regular = @<span class="hljs-string"><span class="hljs-string">"^[a-za-z0-9]+$"</span></span>; Nspredicate *numberandlettertest = [nspredicate predicatewithformat:@<span class="hljs-string"><span class="hljs-string">"self MATCHES%@"</span></span>, regular];<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>[numberandlettertest evaluatewithobject:string];}</code></pre> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Common regular expressions for iOS development</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.