Regular expression in Python
^ |
Matches the beginning of a line |
$ |
Matches the end of the line |
. |
Matches any character |
\s |
matches whitespace |
\S |
matches any non-whitespace |
* |
repeats a character 0 or more times |
*? |
repeats a character 0 or more times(non-greedy) |
+ |
repeats a character 1 or more times |
+? |
repeats a character 1 or more times(non-greedy) |
[aeiou] |
matches a single character in the listed set |
[^XYZ] |
matches a single character not in the listed set |
[a-z0-9] |
the set of characters can include a range |
( |
indicates where string extraction is to start |
) |
indicates where string extraction is to end |
Greedy Matching
import re x='From: Using the :character' y=re.findall('^F.+:', x) print (y) |
Output:
['From: Using the :']
import re x='From: Using the :character' y=re.findall('^F.*:', x) print (y) |
Output:
['From: Using the :']
NON-Greedy Matching
import re x='From: Using the :character' y=re.findall('^F.+?:', x) print (y) |
Output:
['From:']
import re x='From: Using the :character' y=re.findall('^F.*:', x) print (y) |
Output:
['From:']
说明:在字符串'From:Using the :character' 中, 有两个: 号存在。 当要匹配以F开头,以: 号结尾的字符串时,GreedyMatching 会以Greedy 为原则找到尽可能长的匹配字符串。 而NON-Greedy Matching 不会像Greedy 那样去找尽可能长的串。