Python Regular Expression _re module _ using compile acceleration

Source: Internet
Author: User

Use compile acceleration compile (rule [, flag])
  1. Compiles regular rules into a Pattern object for the next use.
  2. The first parameter is regular, and the second parameter is the rule option.
  3. Returns a Pattern object
  4. Use findall (rule, target) directly to match the string, two times a day nothing, if it is repeated use, because the regular engine each time the rules to explain the rule, and the interpretation of the rules is quite time-consuming, so the efficiency is very low. If you want to use the same rule more than once to make a match, you can use the re.compile function to precompile the rule, using the Regular Expression Object or the Pattern that was compiled to return object to be searched.
  5. Cases
  6. >>> s= ' 111,222,aaa,bbb,ccc333,444ddd '
  7. >>> rule=r ' \b\d+\b '
  8. >>> compiled_rule=re.compile (rule)
  9. >>> Compiled_rule.findall (s)
  10. [' 111 ', ' 222 ']
  11. It is seen that using compile rules is similar to using non-compiled uses. The compile function can also specify some rule flags to specify some special options. Multiple options with '|' (bit or) to connect together.
  12. I IGNORECASE ignore case differences.
  13. L LOCAL Character Set localization. This feature is designed to support multiple language versions of the character set using the environment, such as the escape character \w, which stands for [a-za-z0-9] in English, that is, so English characters and numbers. If used in a French environment, the default setting does not match "é " or " C". Plus this L option and you can match it. However, this does not seem to work for the Chinese environment, it still does not match the characters.
  14. M MULTILINE multi-line matching. In this mode ' ^ ' ( representing the beginning of the string ) and ' $ ' ( representing the end of the string ) will be able to match the case of multiple lines, becoming the beginning and end of the line mark. Like what
  15. >>> s= ' 123 456\n789 012\n345 678 '
  16. >>> rc=re.compile (R ' ^\d+ ') # matches a number at the beginning without using the M option
  17. >>> Rc.findall (s)
  18. [' 123 '] # results can only be found at the first beginning of the ' 123 '
  19. >>> rcm=re.compile (R ' ^\d+ ', re. m) # using the m option
  20. >>> Rcm.findall (s)
  21. [' 123 ', ' 789 ', ' 345 '] # found three numbers at the beginning of the line
  22. Similarly, for ' $ ' , without the M option, it will match the last line at the end of the number, i.e. ' 678 ', plus later, it will be able to match three end of the number 456 012 and 678 up .
  23. >>> rc=re.compile (R ' \d+$ ')
  24. >>> rcm=re.compile (R ' \d+$ ', re. M
  25. >>> Rc.findall (s)
  26. [' 678 ']
  27. >>> Rcm.findall (s)
  28. [' 456 ', ' 012 ', ' 678 ']
  29. S Dotall '. ' Will match all the characters. By default '. ' Match all characters except the newline character ' \ n ' , after using this option,'. ' Can match any character that includes ' \ n ' .
  30. U     unicode          \w ,   \w   \b   \b   \d   \d   \s   and   will use Unicode.
  31. X VERBOSE This option ignores whitespace in the regular expression and allows you to use ' # ' to guide a comment. This will allow you to write the rules more beautifully. Like you can put the rules
    1. >>> rc = Re.compile (r "\d+|[ a-za-z]+ ") #匹配一个数字或者单词
    1. Use the X option to write:
    1. >>> rc = Re.compile (R "" "# Start a rule
    1. \d+ #
    1. | [A-za-z]+ # Word
    1. "" ", Re. VERBOSE)
    1. In this mode, if you want to match a space, you must use the form ' \ ' (followed by a space)

Python Regular Expression _re module _ using compile acceleration

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.