iOS Regular expression

Source: Internet
Author: User
Tags uppercase letter

Problem Introduction

1. To verify that the password length entered by the user satisfies the length of the 6~18 bit

^. {6,18}$

2. Fixed phones are 0 area code-eight-digit format, then the regular expression matches the following

^0\\d{2}\-?\\d{8}$

3. Authentication for password strength. Today's passwords should include one or more uppercase and lowercase letters, and the match for this regular expression is

(^.*[a-z]+.*[a-z]+.*$|^.*[a-z]+.*[a-z]+.*$)

Symbolic parsing

1.{6,18} represents 6-18 bits. where the {n} match character repeats n times, {n,} matches repeats n or more times, and {n,m} matches repeats n to M times.

2. ^ and $ belong to the special symbol, ^ represents the beginning of the matching string, and $ represents the end of the matching string: the symbol represents any single character (except the newline character), \d is used to represent any individual number, \w represents any one letter or number, or any number directly using 0-9 to represent a specific number. and the special symbol? Indicates that the previous character is 0 or 1, * indicates that the previous value was repeated 0 or more times.

{6,18} This special symbol represents the number of digits represented by the previous symbol. Combine to say ^. {6,18}$ matches a string of 6 to 18 bits in length

We can put

^.*[a-z]+.*[a-z]+.*$|^.*[a-z]+.*[a-z]+.*$

Split into ^.*[a-z]+.*[a-z]+.*$ and ^.*[a-z]+.*[a-z]+.*$.

How to split expressions: I divide the characters of expressions into two categories: value expression and cosmetic expression. The so-called value expression means that the symbol represents a value, just as \d represents a number. Represents any non-newline character. A decorated expression is used to modify a value to achieve a condition, such as {2}, which indicates that the previous value is repeated two times.

^.*[a-z]+.*[a-z]+.*$ can be split into parts: ^$,. *, [a-z]+, [a-z]+.

^$ will not say more.

. * Here's what we're going to show you. * indicates that the previous value symbol repeats any number of times.

[a-z]+-Represents a closed set of values formed from the left-hand value to the value on the right, and [] the value in square brackets must be a subset of the middle set of parentheses, note that there can be multiple sets in parentheses, such as [a-z0-9a-z] to match any uppercase or lowercase letter or number; + Represents a duplicate value of at least one.

In conjunction with the above parsing, the ^.*[a-z]+.*[a-z]+.*$ means to start with any number of characters followed by an uppercase letter, with any number of characters followed by an uppercase letter, and a lowercase letter, followed by any number of numbers, letters, or characters. The ^.*[a-z]+.*[a-z]+.*$ represents the position of any number of characters in the lowercase letters before the uppercase letters, and the two combinations match to ensure that the string includes at least one lowercase letter and one upper case.

PS: One thing to note is that the expression shown above \d These special symbols in our code more than one \, this is because \ itself is an escape symbol, in order to ensure that the expression can be properly matched, we have to give \ One escape, so it becomes \ \. Basically all symbol characters need to be escaped.

Syntax/Character Description table

Value expression

. Match any character except line break

\w characters that match letters or numbers

\w matches any character that is not a letter or number

\s matches any white space character (space, tab, line feed)

\s matches any character that is not a white letter

\d matches any number

\d matches any non-numeric character

\b A character that matches the end or beginning of a word

\b matches any character that is not the end or beginning of a word

[^x] matches any character that is not X. Any character that matches a non-lowercase letter, such as [^[a-z]]

^ matches the beginning of the string

$ matches the end of a string

Modified expression

* Match repeat any number of times

+ Repeat the number of times more than once

? Match once or 0 times

{n} match repeats n times

{n,} matches repeat n or n more times

{n,m} match repeats at least N times up to M times



Reference:
Original link: http://www.jianshu.com/p/00da4d87b777

iOS Regular expression

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.