Skip to main content
 首页 » 编程设计

python之如何使用正则表达式匹配请求行

2025年05月04日23shanyou

request line definition: 
request_line := request_method + spaces + any number of other characters 
 
eg: some valid request lines:  
  GET /user/1 
  POST /users 
  PUT ABC 
  DELETE XYZ 

那么我的问题是如何匹配请求行呢?我尝试了以下方法,但不幸的是它不起作用。

re.compile('^[GET|POST|PUT|DELETE]\s+.+').match(request_line) # output None 

请您参考如下方法:

您使用 [] 来匹配一组特定的字符 例如 [a-zA-Z] 将匹配所有大写和小写字符。

对于你的问题你必须使用组

试试这个

re.compile('^(GET|POST|PUT|DELETE)\s+.+').match(request_line)