Realization of Python Using Regular Expression to Segment String
- 2021-07-22 10:02:16
- OfStack
As follows:
re.split(pattern, string, [maxsplit], [flags])
pattern: Represents a pattern string converted from the regular expression to match.
string: Represents the string to match.
maxsplit: Optional parameter indicating the maximum number of splits.
flags: An optional parameter representing a flag bit is used to control the matching method, such as whether the numeric mother is capitalized or not
Sample code:
import re
pattern = r'[?|&]' # Define Delimiter
url = 'http://www.baidu.com/login.jsp?username="wei"&pwd="123"' # String to be split
result = re.split(pattern, url) # With pattern Value of Split string
print(result)
Implementation results:
['http://www.baidu.com/login.jsp', 'username="wei"', 'pwd="123"']
Common signs:
标志 | 说明 |
---|---|
A 或 ASCII | 对于\w、\W、\b、\B、\d、\D、\s和\S只进行ASCII匹配(仅适用于Python3.x) |
I 或 IGNORECASE | 执行不区分子母大小写的匹配 |
M 或 MULTILINE | 将^和$用于包括整个字符串的开始和结尾的每1行(默认情况下,仅适用于整个字符串的开始和结尾处) |
S 或 DOTALL | 使用(.)字符匹配所有字符,包括换行符 |
X 或 VERBOSE | 忽略模式字符串中未转义的空格和注释 |