- >>> s='str1 | str2 & str3 &&|& str4'
-
>>> import re
-
>>> re.compile(r'[\s|&]+')
-
<_sre.SRE_Pattern object at 0x100461db0>
-
>>> regex=re.compile(r'[\s|&]+')
-
>>> regex.split(s)
-
['str1', 'str2', 'str3', 'str4']
-
>>>
special characters:'.' In the default mode, this matches any character except a newline.
'^' Matches the start of the string.
'*' Causes the resulting RE to match 0 or more repetitions of the preceding RE.
'ab*' will match 'a','ab' or a followed by any number of 'b's.
'+‘ Causes the resulting RE to match 1 or more repetitions of the preceding RE.
'ab+' will match 'a' followed by any non-zero number of 'b's.
'?' cause the resulting RE to match 0 or 1 repetitions of the preceding RE.
'ab?' will match either 'a' or 'ab'
'$' match the end of the string of just the newline at the end of the string.
{m} Specifies that exactly m c
opies of the previous RE should be matched.
a{6} will match exactly six 'a' characters.
{m, n} match from m to n repetitions of the preceding RE, attempting to match
as many repetitions as possible.
{m, n}? Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as few repetitions as possible.this is the non-greedy version of the previous qualifier. 'aaaaaa', a{3,5} will match 5 'a' characters, whilea{3,5}? will only match 3 characters
[] Used to indicate a set of characters. Characters can be listed individually, or a range of characters can be indicated by giving two characters and
separating them by a '-'. Special characters are not active inside sets. For example, [akm$] will match any of the characters 'a', 'k', 'm', or '$';
[a-z] will match any lowercase letter, and [a-zA-Z0-9] matches any letter or digit. Character classes such as \w or \S (defined below) are also
acceptable inside a range, although the characters they match depends on whether LOCALE or UNICODE mode is in force. If you want to include a ']' or
a '-' inside a set, precede it with a backslash, or place it as the first character. The pattern []] will match ']', for example.
You can match the characters not within a range by complementing the set. This is indicated by including a '^' as the first character of the set; '^'
elsewhere will simply match the '^' character. For example, [^5] will match any character except '5', and [^^] will match any character except '^'.
Note that inside [] the special forms and special characters lose their meanings and only the syntaxes described here are valid. For example, +, *, (, )
, and so on are treated as literals inside [], and backreferences cannot be used inside [].
阅读(766) | 评论(0) | 转发(0) |