python 正则匹配bytes
作者:互联网
网上找了半天都只有关于字符串的正则匹配,想到bytes的数据前面加了b,那么在匹配时是不是也要呢?直接上示例:
import re
data=b'blob 2199\x00<?php\n\nif (isset($_GET[\'page\'])) {\n\t$page = $_GET[\'page\'];\n} else {\n\t$page = "home";\n}\n\n$file = "templates/" . $page . ".php";\n\n// I heard \'..\' is dangerous!\nassert("strpos(\'$file\', \'..\') === false") or die("Detected hacking attempt!");\n\n// TODO: Make this look nice\nassert("file_exists(\'$file\')") or die("That file doesn\'t exist!");\n\n?>\n<!DOCTYPE html>\n<html>\n\t<head>\n\t\t<meta charset="utf-8">\n\t\t<meta http-equiv="X-UA-Compatible" content="IE=edge">\n\t\t<meta name="viewport" content="width=device-width, initial-scale=1">\n\t\t\n\t\t<title>My PHP Website</title>\n\t\t\n\t\t<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />\n\t</head>\n\t<body>\n\t\t<nav class="navbar navbar-inverse navbar-fixed-top">\n\t\t\t<div class="container">\n\t\t \t<div class="navbar-header">\n\t\t \t\t<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">\n\t\t \t<span class="sr-only">Toggle navigation</span>\n\t\t \t<span class="icon-bar"></span>\n\t\t \t<span class="icon-bar"></span>\n\t\t \t<span class="icon-bar"></span>\n\t\t \t</button>\n\t\t \t<a class="navbar-brand" href="#">Project name</a>\n\t\t </div>\n\t\t <div id="navbar" class="collapse navbar-collapse">\n\t\t \t<ul class="nav navbar-nav">\n\t\t \t<li <?php if ($page == "home") { ?>class="active"<?php } ?>><a href="?page=home">Home</a></li>\n\t\t \t<li <?php if ($page == "about") { ?>class="active"<?php } ?>><a href="?page=about">About</a></li>\n\t\t \t<li <?php if ($page == "contact") { ?>class="active"<?php } ?>><a href="?page=contact">Contact</a></li>\n\t\t\t\t\t\t<!--<li <?php if ($page == "flag") { ?>class="active"<?php } ?>><a href="?page=flag">My secrets</a></li> -->\n\t\t \t</ul>\n\t\t </div>\n\t\t </div>\n\t\t</nav>\n\t\t\n\t\t<div class="container" style="margin-top: 50px">\n\t\t\t<?php\n\t\t\t\trequire_once $file;\n\t\t\t?>\n\t\t\t\n\t\t</div>\n\t\t\n\t\t<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js" />\n\t\t<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js" />\n\t</body>\n</html>'
data = re.sub(rb'blob \d+\x00',b'',data)
# data=re.findall(rb'blob \d+\x00',data)
print(data)
标签:python,bytes,re,正则,blob,active,tclass,x00,data 来源: https://blog.csdn.net/zx520113/article/details/119058996