Python--Regular learning

Source: Internet
Author: User

Regular expression syntax for re
The Regular Expression syntax table is as follows:

Re.match

Re.match tries to match a pattern from the beginning of the string , and matches the match object successfully, otherwise none is returned. Such as: The following example matches the first word.

Additional notes:

We can query the group by M.group (number). Group (0) is the entire regular expression of the search results, group (1) is the first group ...

Import rem = Re.search ("Output_ (\d{4})", "Output_1986.txt") Print (M.group (1))

We can also name the group to better use the M.group query:

Import rem = Re.search ("Output_ (? P<YEAR>\D{4}) "," Output_1986.txt ")   # (? P<name>, ...) Name Print for group (M.group ("Year"))

Example 1

Re.search

Re.search scans the entire string and returns the first successful match.

" Jgood is a handsome boy, he's cool, clever, and so on ... "  = Re.search (R'\shan (ds) ome\s', text)if  m:    Print M.group (), M.group (1)else:    'notsearch  '

the difference between Re.match and Re.search:Re.match matches only the beginning of the string, if the string starts not conforming to the regular expression, the match fails, the function returns none, and the Re.search matches the entire string until a match is found.

Re.sub

The re.sub is used to replace a match in a string. The following example replaces a space in a string with a '-':

" Jgood is a handsome boy, he's cool, clever, and so on ... " Print re.sub (r'\s+'-', text)
Print re.sub (R ' \s ', Lambda m: ' [' + m.group (0) + '] ', text, 0)

Output:

Jgood-is-a-handsome-boy,-he-is-cool,-clever,-and-so-on ...

jgood[]is[]a[]handsome[]boy,[]he[]is[]cool,[]clever,[]and[]so[]on ...

Re.split

You can use Re.split to split a string, such as: Re.split (R ' \s+ ', text), and divide the string into a word list by space.

Re.findall

Re.findall can get all the matching strings in the string. such as: Re.findall (R ' \w*oo\w* ', text); Gets all the words in the string that contain ' oo '.

Re.compile

You can compile a regular expression into a regular expression object. It is possible to compile regular expressions that are often used as regular expression objects, which can improve some efficiency. Here is an example of a regular expression object:

Re.sub's function prototype is: re.sub (Pattern, REPL, string, count)

Where the second function is the replaced string, in this case '-'

The fourth parameter refers to the number of replacements. The default is 0, which means that each match is replaced.

Re.sub also allows for complex processing of replacements for matches using functions. such as: Re.sub (R ' \s ', Lambda m: ' [' + m.group (0) + '] ', text, 0); Replace the space in the string ' ' with ' [] '.

Python--Regular learning

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.