Python regular expression [1]

Source: Internet
Author: User
Here we also want to mention that the quantifiers in the regular expression involve greedy and non-greedy patterns. Greedy means getting the maximum value and matching as much as possible. Non-greedy mode is the opposite (greedy mode by default ). Example: This article describes the Python regular expression.

If you don't talk much about it, start with the simplest one:

'.': Can match any single character (point) except the line break ).

'*' Can match the previous subexpression zero or multiple times (an asterisk ).

Therefore, the above two combinations '. *' (dot and star) match all except line breaks.

'+': Repeat once or more times.

'? ': Zero or one repeat.

'\ D': matches a number character. It is equivalent to [0-9].

'\ W' matches any word characters that contain underscores. It is equivalent to '[A-Za-z0-9 _]'.

'/S' matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [\ f \ n \ r \ t \ v]

__^ _ ^ 'Matches the start position of the input string.

'$' Matches the end position of the input string.

The above are frequently used. Of course there are still many frequently used ones. please refer to the manual when necessary.

The description is not intuitive enough to directly repeat the experiment. To use a regular expression in Python, you can import the record directly:

>>> Import re

>>>

First try to match all:

>>> vlan = 'switchport access vlan 612'>>> ljds = re.search('.*',vlan).group()>>> ljds'switchport access vlan 612'

Try matching numbers again:

>>> ljds = re.search('\d',vlan).group()>>> ljds'6'

Because '/d' matches a number, you can add' {3} 'to match '123' and three numbers }':

>>> ljds = re.search('\d{3}',vlan).group()>>> ljds

'123'

Similarly, if you want to match 13 characters (including spaces ):

>>> ljds = re.search('[\w\s]{13}',vlan).group()>>> ljds'switchport ac'

Here we also want to mention that the quantifiers in the regular expression involve greedy and non-greedy patterns. Greedy means getting the maximum value and matching as much as possible. Non-greedy mode is the opposite (greedy mode by default ). Example:

The above match 13 characters. if it matches 2 to 10 characters, it is written as '[\ w \ s] {2, 10, so are two or ten matched? Because the default mode is greedy, it will match the maximum value:

>>> ljds = re.search('[\w\s]{2,10}',vlan).group()>>> ljds'switchport'

Add a question mark after the quantifiers '? ', It switches to the non-greedy mode, that is, the minimum matching:

>>> ljds = re.search('[\w\s]{2,10}?',vlan).group()>>> ljds'sw'

Next we will introduce "capture:

(Exp): matches exp.

(? = Exp): match the position before exp.

(? <= Exp): matches the position after exp.

>>> Vlan = 'switchport access vlan 100'

Basic:

>>> ljds = re.search('(access)',vlan).group()>>> ljds'access'

Match any character before 'access:

>>> ljds = re.search('.*(?=access)',vlan).group()>>> ljds'switchport '

Match any character after 'vlan:

>>> ljds = re.search('(?<=vlan).*',vlan).group()>>> ljds' 612'

OK. here, let's take a look at the regular expression of the previously captured vro name:

DeviceName = re.search('.*(?=#show run)',telreply).group()

The above is the content of the Python regular expression [1]. For more information, see PHP Chinese Web (www.php1.cn )!

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.