python:twistedlxmlandre
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python:twistedlxmlandre [2014/08/11 06:51] – [Build xml using Etree] admin | python:twistedlxmlandre [2022/10/29 16:15] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 527: | Line 527: | ||
| </ | </ | ||
| ===== re Package(Regular Expression) ===== | ===== re Package(Regular Expression) ===== | ||
| + | To use re package, we need to import it:<code python> | ||
| + | import re | ||
| + | </ | ||
| ==== Regular Expression Language ==== | ==== Regular Expression Language ==== | ||
| A regular expression (abbreviated regex or regexp) is a sequence of characters that forms a search pattern\\ | A regular expression (abbreviated regex or regexp) is a sequence of characters that forms a search pattern\\ | ||
| - | refer: http:// | + | refer: |
| + | * http:// | ||
| + | * python: https:// | ||
| **Match Character** | **Match Character** | ||
| Line 571: | Line 576: | ||
| </ | </ | ||
| === re.findall === | === re.findall === | ||
| - | * re.findall: The findall() is probably the single most powerful function in the re module< | + | findall: The findall() is probably the single most powerful function in the re module |
| + | - Example 1: <code python> | ||
| str = ' | str = ' | ||
| Line 580: | Line 586: | ||
| # do something with each found email string | # do something with each found email string | ||
| print email | print email | ||
| - | </ | + | </ |
| + | * [\w\.-]+ => Begin with one or multiple(sign: | ||
| + | * @[\w\.-]+ => next of it is character @ and one or multiple characters in group: [word, **.** , **-**] | ||
| + | - Example 2: <code python> | ||
| # Open file | # Open file | ||
| f = open(' | f = open(' | ||
| Line 683: | Line 692: | ||
| text2 = re.sub(" | text2 = re.sub(" | ||
| print text2 | print text2 | ||
| + | </ | ||
| + | Python for beginner is a very good website | ||
| </ | </ | ||
| * Here is another example (taken from Googles Python class ) which searches for all the email addresses, and changes them to keep the user (1) but have yo-yo-dyne.com as the host.< | * Here is another example (taken from Googles Python class ) which searches for all the email addresses, and changes them to keep the user (1) but have yo-yo-dyne.com as the host.< | ||
| Line 691: | Line 702: | ||
| ## 1 is group(1), 2 group(2) in the replacement | ## 1 is group(1), 2 group(2) in the replacement | ||
| - | print re.sub(r' | + | print re.sub(r' |
| ## purple alice@yo-yo-dyne.com, | ## purple alice@yo-yo-dyne.com, | ||
| + | </ | ||
| + | purple alice@yo-yo-dyne.com, | ||
| </ | </ | ||
| * re.compile: With the re.compile() function we can compile pattern into pattern objects, which have methods for various operations such as searching for pattern matches or performing string substitutions. | * re.compile: With the re.compile() function we can compile pattern into pattern objects, which have methods for various operations such as searching for pattern matches or performing string substitutions. | ||
python/twistedlxmlandre.1407739909.txt.gz · Last modified: (external edit)
