Examples of the legality of Python regular detection ciphers

Source: Internet
Author: User
Tags character set lowercase regular expression in python

Before writing the solution, make a list

Special characters in Python regular expressions:
^ indicates that matching characters must be at the front
$ indicates that matching characters must be on the last side
* Match * Previous characters 0 times or n times
+ Match + previous character 1 times or n times
? Match the previous character 0 or 1 times
. (decimal point) matches all characters except line breaks
(x) match x and record matching values
X|y match x or Y
{n} here n is a positive integer. Match the preceding n characters
{N,} here n is a positive integer. Match at least n preceding characters
{N,m} here N and M are positive integers. Match at least N, up to M preceding characters
A list of [XYZ] characters that matches any character in a table and can be used to indicate a range of characters, such as [A-z], for all lowercase characters
[b] matches a space
b matches the line of a word, such as a space
B matches the non dividing line of a word
Re module matching rule (third parameter of the Re.match function):
Re. IGNORECASE ignores case in the text
Re. LOCALE processing Character Set localization
Re. MULTILINE support for multiple line matches
Re. Dotall matches some special tags, such as the use of. Matching N and other characters
Re. VERBOSE ignore spaces in regular expressions or line-wrapping characters
Re. Unicode encoding with Unicode

Customer system upgrades, requires the user password to meet certain rules, namely: uppercase and lowercase letters, numbers, symbols, length of not less than 8, so first in Python wrote a simple test program:

#encoding =utf-8

#-------------------------------------------------------------------------------
# Name: Module 1
# Purpose:
#
# Author:administrator
#
# created:10-06-2014
# Copyright: (c) Administrator 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
Import re

def checklen (PWD):
Return Len (PWD) >=8

def checkcontainupper (PWD):
Pattern = Re.compile (' [a-z]+ ')
Match = Pattern.findall (pwd)

If match:
Return True
Else
Return False

def checkcontainnum (PWD):
Pattern = Re.compile (' [0-9]+ ')
Match = Pattern.findall (pwd)
If match:
Return True
Else
Return False

def checkcontainlower (PWD):
Pattern = Re.compile (' [a-z]+ ')
Match = Pattern.findall (pwd)

If match:
Return True
Else
Return False

def checksymbol (PWD):
Pattern = Re.compile (' ([^a-z0-9a-z]) + ')
Match = Pattern.findall (pwd)

If match:
Return True
Else
Return False

def checkpassword (PWD):

#判断密码长度是否合法
Lenok=checklen (PWD)

#判断是否包含大写字母
Upperok=checkcontainupper (PWD)

#判断是否包含小写字母
Lowerok=checkcontainlower (PWD)

#判断是否包含数字
Numok=checkcontainnum (PWD)

#判断是否包含符号
Symbolok=checksymbol (PWD)

Print (Lenok)
Print (Upperok)
Print (Lowerok)
Print (Numok)
Print (Symbolok)
Return (Lenok and Upperok and Lowerok and Numok and Symbolok)


def main ():
If Checkpassword (' helloworld#123 '):
Print (' Test pass ')
Else
Print (' Detect failed ')


if __name__ = = ' __main__ ':
Main ()

Usually use is not much, do not know how to write a regular to meet the requirements, with a more stupid way, who knows the way a regular test please enlighten!

Seven usage examples of regular expressions

1. The original type string in Python

The Python compiler is a ' \ ' (backslash) to represent an escape character in a string constant.

If the backslash is followed by a special character that a string of compilers can recognize, the entire escape sequence is replaced with the corresponding special character (for example, ' \ n ' will be replaced by the compiler with a newline character).

But this poses a problem for using regular expressions in Python, because the backslash is also used in the ' re ' module to escape special characters (such as * and +) in regular expressions.

The combination of these two ways means that sometimes you have to escape the escape character itself (when special characters are RP by Python and regular expression compilers), but at other times you don't have to do this (if special characters can only be identified by the Python compiler).

Instead of putting our minds to figuring out how many backslashes are needed, we can use the original string instead.

The original type string can be created simply by adding a character ' R ' before the double quotation mark in the normal string. When a string is of the original type, the Python compiler does not attempt to make any substitutions to it. Essentially, you're telling the compiler not to interfere with your strings at all.

>>> string = ' This is A\nnormal string '
>>> rawstring = R ' and this is A\nraw string '
>>> Print String
This is an ordinary string
>>> Print rawstring
And this is A\nraw string
This is an original type string.


Using regular expressions in Python to find

The ' re ' module provides several methods for exactly querying the input string. The methods we will be discussing are:

Re.match ()
Re.search ()
Re.findall ()
Each method receives a regular expression and a string to find a match. Let's look at each of these methods in more detail to figure out how they work and how different they are.

2. Use Re.match Lookup – match start

Let's take a look at the match () method first. The match () method works by finding a matching object only when the start of the search string matches the pattern.

For example, the Mathch () method is invoked on the string ' Dog cat dog ', and the lookup pattern ' dog ' will match:

>>> Re.match (R ' Dog ', ' dog cat dog ')
<_sre. Sre_match Object at 0xb743e720<
>>> match = Re.match (R ' Dog ', ' dog cat dog ')
>>> Match.group (0)
' Dog '


We'll talk more about the group () method later. Now we just need to know that we called it with 0 as its arguments, and the group () method returns the matching pattern found.

I'm also going to skip over the returned Sre_match object and we'll discuss it soon.

However, if we call the math () method on the same string, find the pattern ' cat ', the match will not be found.

>>> Re.match (R ' Cat ', ' dog cat dog ')
>>>

3. Use Re.search Lookup – match any location

The search () method is similar to match (), but the search () method does not restrict us from finding matches only from the beginning of the string, so finding ' cat ' in our sample string finds a match:

Search (R ' Cat ', ' dog cat dog ')
>>> Match.group (0)
' Cat '

However, the search () method stops searching after it finds a match, so finding ' dog ' in our sample string using the Searc () method finds only its first occurrence.

>>> match = Re.search (R ' Dog ', ' dog cat dog ')
>>> Match.group (0)
' Dog '


4. Use re.findall– all matching objects

The most common search method I've used so far in Python is the FindAll () method. When we call the FindAll () method, we can very simply get a list of all the matching patterns, rather than get the match object (we'll talk more about the match object in the next step). It's easier for me. Call the FindAll () method on the sample string we get:

[' Dog ', ' dog ']
>>> Re.findall (R ' Cat ', ' dog cat dog ')
[' Cat ']


5. Using Match.start and Match.end methods

So what did the previous search () and Match () methods have previously returned to our ' match ' object?

Unlike the simple matching part of the return string, search () and match () return the matching object, which is actually a wrapper class about the matching substring.

Previously you saw that I could get a matching substring by calling the group () method (we'll see in the next section that the fact that the matching object is useful when dealing with group problems), but the matching object also contains more information about the matching substring.

For example, the match object can tell us where the matching content begins and ends in the original string:

>>> match = Re.search (R ' Dog ', ' dog cat dog ')
>>> Match.start ()
0
>>> Match.end ()
3

Knowing this information is sometimes very useful.

6. Use Mathch.group to GROUP by numbers

As I mentioned earlier, matching objects are handy when working with groups.

Grouping is the ability to position a specific substring of an entire regular expression. We can define a grouping as part of the entire regular expression, and then individually locate the content that corresponds to that part.

Let's take a look at how it works:

>>> contactinfo = ' Doe, john:555-1212 '


The string I just created resembles a fragment taken out of someone's address book. We can match this line with a regular expression:

>>> Re.search (R ' \w+, \w+: \s+ ', ContactInfo)
<_sre. Sre_match Object at 0xb74e1ad8<


By enclosing the specific parts of the regular expression with parentheses (characters ' and '), we can group the content and then separate the subgroups.

>>> match = Re.search (R ' (\w+), (\w+): (\s+) ', ContactInfo)

These groupings can be obtained by using the group () method of the grouped objects. They can be positioned from the numeric order in which they appear from left to right in the regular expression (starting from 1):

>>> Match.group (1)
' Doe '
>>> Match.group (2)
' John '
>>> Match.group (3)
' 555-1212 '

The reason the group's ordinal number starts at 1 is because the No. 0 Group is reserved for all matching objects (we learned the match () method and the search () method before we saw it).

>>> Match.group (0)
' Doe, john:555-1212 '

7. Use Match.group to group by alias

Sometimes, especially when a regular expression has many groupings, it becomes impractical to locate the group by its order of occurrence. Python also allows you to specify a group name by using the following statement:

>>> match = Re.search (R ') (? p<last>\w+), (? p<first>\w+): (? p<phone>\s+) ', ContactInfo

We can still use the group () method to get the grouped content, but at this point we want to use the name we specify rather than the number of places the group used previously.

>>> match.group (' last ')
' Doe '
>>> match.group (' a ')
' John '
>>> match.group (' phone ')
' 555-1212 '

This greatly enhances the clarity and readability of the code. You can imagine that when regular expressions become more complex, it becomes more and more difficult to understand what a group is and what it captures. The naming of your group will clearly tell you and your readers your intentions.

Although the FindAll () method does not return a grouped object, it can also use grouping. Similarly, the FindAll () method returns a set of tuples in which the nth element in each tuple corresponds to the nth grouping in the regular expression.

>>> Re.findall (R ' (\w+), (\w+): (\s+) ', ContactInfo)
[(' Doe ', ' John ', ' 555-1212 ')]

However, naming a group does not apply to the FindAll () method.

In this article we introduce some of the basics of using regular expressions in Python. We learned about the original string type (and what it can do to solve some headaches in using regular expressions). We also learned how to use match (), search (), and FindAll () methods for basic queries, and how to use groupings to handle child components of matching objects.

Related Article

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.