How to delete a Python Regular Expression

Source: Internet
Author: User

Python regular expressions have a wide range of application in Python. We cannot use them better because we do not know their true meaning. In fact, we can continue to improve our use in the future only by better learning.

Python Regular Expression (Regex) is a single string used to describe or match a series of strings that conform to a certain syntax rule. Regular Expressions are a powerful tool for processing text. Deleting code line numbers is a pile of dishes. The Code is as follows:

 
 
  1. Import re # import the Python Regular Expression Module
  2. Def CutLineNum (inStr ):
  3. MultiStr = inStr. splitlines (1) outStr = u''
  4. P = re. compile (R' \ D * \ d + \ s? [. |: | \] | \)]? \ S? ') # Compile a regular expression
  5. For singleLine in multiLine: outStr + = p. sub ('', singleLine,
    Count = 1) # Replace the first character in each line that matches the Regular Expression
  6. Eturn outStr # returns the accumulated string

The coffee section in the above Code is a Regex, which means: 0 or multiple non-numeric characters + at least 1 digit character + 0 or 1 blank character + 0 or 1 separator (including dot, colon, Right circle/square brackets, other separators can be added) + 0 or 1 blank character.

The above Regex complies with the rules of most pre-code row numbers. Deleting a row number replaces the first string matching the Regex in each line of code with a null character, because the program may have abc1.open and other conditions, if all matches, the code will be deleted by mistake ).

Continue Optimization-use function Programming

It doesn't seem concise enough. Python regular expressions are a quasi-Functional Programming, FP) language, which is the best way to simplify code. Here, I use two function programming methods-Anonymous function lambda) and a high-level Function map) to simplify the code to one line, as shown below:
 

 
 
  1. Import re # import the Python Regular Expression Module
  2. OutStr = ''. join (map (lambda x: re. compile (R' \ D * \ d + \ s? [.
    |: | \] | \)]? \ S? '). Sub ('', x, count = 1), inStr. splitlines (1 ))))

The above is a detailed introduction to Python regular expressions. In this way, the code is optimized to only one line. Is it very concise? Of course, the readability of the Code is affected. Therefore, function programming cannot be abused. For examples of abuse, see my article "Learning Python list connotation: one line of code to get double supercube computing"). Function programming is a slightly complicated issue. I think it is okay to master or not, so I will not explain the above Code in detail.

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.