Python base day-13[module: RE not finished]

Source: Internet
Author: User

Re

is essentially a small language.

The object to manipulate is a string.

Re.findall (): A list is returned. Matches all elements that meet the criteria.

Re.search (): An object is returned. Matches only the first element found, and if the match succeeds then the returned object is the index range with the matching target, and the matching content. The match fails to return none.

Re.match (): Return is also an object as search, matching only the beginning of the file position. If the match succeeds then the returned object is the index range with the matching target, and the matching content. The match fails to return none.

Example: S='abclaskdjaklsjdlasdjabcasd123lksdlasd0asdasdaaa'Res= Re.match ('ABC', s)Print(Res,res.group ()) Res= Re.search ('AAA', s)Print(Res,res.group ()) Res= Re.findall ('ABC', s)Print(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ practice. PY<_sre. Sre_match object; Span= (0, 3), match='ABC'>ABC<_sre. Sre_match object; Span= (match=),'AAA'>aaa['ABC','ABC']process finished with exit code 0

Metacharacters

.: You can refer to any symbol except a newline character.

ImportRes='abclaskdjaklabcsjdlasdjaba1ccasd123a/clksdlasd0asdasdaaa'Res= Re.findall ('A.C', s)Print(res) Res= Re.findall ('A.. C', s)Print(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ practice. py['ABC','ABC','A1c','A/ c']['a1cc']process finished with exit code 0

^: matches only the beginning of the string.

ImportRes='abc1231321abclskdjlsk654sdsd3a13213a'S1='32132ABC'Res= Re.findall ('^ABC', s)Print(res) Res= Re.findall ('^ABC', S1)Print(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ practice. py['ABC'][]process finished with exit code 0

$: Matches only the end of the string.

ImportRes='abc1231321abclskdjlsk654sdsd3a13213a'S1='32133ABC'Res= Re.findall ('3a$', s)Print(res) Res= Re.findall ('3a$', S1)Print(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ practice. py['3a'][]process finished with exit code 0

*: The first character of the symbol repeats 0-infinity.

ImportRes='ABCCCCCCABCCDLLKJABC'Res= Re.findall ('abc*', s)Print(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ practice. py['ABCCCCCC','ABCC','ABC']process finished with exit code 0

+: The first character of the symbol repeats 1-infinity.

ImportRes='ABCCCCCCABCCDLLKJABCABABABC'Res= Re.findall ('abc+', s)Print(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ practice. py['ABCCCCCC','ABCC','ABC','ABC']process finished with exit code 0

?: the symbol is repeated 0-1 times before a character.

ImportRes='ABCCCCCCABCCDLLKJABCABABABC'Res= Re.findall ('ABC?', s)Print(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ practice. py['ABC','ABC','ABC','AB','AB','ABC']process finished with exit code 0

{}: The first character of the symbol specifies the number of repetitions.

ImportRes='ABCCCCCCABCCDLLKJABCCCABABABC'Res= Re.findall ('abc{3,4}', s)Print(res) Res= Re.findall ('abc{5}', s)Print(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ practice. py['ABCCCC','ABCCC']['ABCCCCC']process finished with exit code 0

\: Escape character, which converts the original meaning of the following characters.

Escape characters are stripped of their functionality when combined with metacharacters. It is also possible to turn some ordinary symbols into special meanings with special ordinary symbols.

Common symbols that can be converted:

\d  matches any decimal number,      which is equivalent to the class [0-9]\d  matches any non-numeric character; it is equivalent to a    class [^0-9]\s  matches any whitespace character; it is equivalent to a      class [\t\n\r \f\v]\s  matches any non-whitespace character; it is equivalent to a    class [^ \t\n\r\f\v]\w  matches any alphanumeric character;   it is equivalent to class [a-za-z0-9_]\w  matches any non-alphanumeric character; it is equivalent to a class [^a-za-z0-9_]\b  matches a special character boundary, such as a space, &,#,etc.

(): Group. Fix some content into one item.

ImportRes='RABHDG8S99D222A1'ret=re.findall ('(AB) |\d+', s)Print(ret) execution result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ practice. py['AB',"',"',"',"']process finished with exit code 0 (): There is also a priority display function, so the above numbers are not shown. The following solutions are adopted:ImportRes='RABHDG8S99D222A1'ret=re.findall (' (?: AB)|\d+', s)Print(ret) execution result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ practice. py['AB','8',' About','222','1']process finished with exit code 0

Famous groups:

Import'rabhdg8s99d222a1'ret=re.search ('(? P<TEST>AB)', S'print(Ret.group ('test' ) ) execution Result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ exercise. Pyabprocess finished with exit Code 0

[]: Character set (multiple selection) matches only one character in a character set.

In the character set only ^ \-also has its own special meaning, the other is the normal character set, the ^ in the character set does not represent the beginning of the match but the meaning of inversion.

ImportRes='SDNASKDABCASPDLABEFLA1231KSAAABBCPAS213LSDCBCABLKC'Res= Re.findall ('Ab[ce]', s)Print(res) Res= Re.findall ('[a-z]+', s)Print(res) Res= Re.findall ('[0-9]+', s)Print(res) Res= Re.findall ('[^a-z]+', s)Print(res) Res= Re.findall ('[\d]+', s)Print(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ practice. py['ABC','Abe']['Sdnaskdabcaspdlabefla','Ksaaabbcpas','LSDCBCABLKC']['1231','213']['1231','213']['1231','213']process finished with exit code 0

|: or meaning.

ImportRes='SDNASKDABCASPDLABEFLA1231KSAAABBCPAS213LSDCBCABLKC'Res= Re.findall ('a.c|\d+', s)Print(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-14/day14_ practice. py['ABC','1231','213']process finished with exit code 0

Python base day-13[module: RE not finished]

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.