{"id":32,"date":"2018-11-18T18:42:00","date_gmt":"2018-11-18T10:42:00","guid":{"rendered":"http:\/\/47.107.162.66\/?p=32"},"modified":"2023-06-26T21:42:45","modified_gmt":"2023-06-26T13:42:45","slug":"re","status":"publish","type":"post","link":"https:\/\/harson.co\/index.php\/2018\/11\/18\/re\/","title":{"rendered":"RegularExpression"},"content":{"rendered":"<h2>RE\u57fa\u672c\u8bed\u6cd5<\/h2>\n<p>. \u8868\u793a\u4efb\u4f55\u4e00\u4e2a\u5b57\u7b26<\/p>\n<p>[a-z]\u8868\u793a\u4efb\u4f55\u4e00\u4e2a\u5c0f\u5199\u5b57\u6bcd\u00a0 [A-Z] \u8868\u793a\u4efb\u4f55\u4e00\u4e2a\u5927\u5199\u5b57\u6bcd<\/p>\n<p>[^0-9] \u8868\u793a\u9664\u6570\u5b57\u7684\u5b57\u7b26\u00a0 ^ \u8868\u793a\u6392\u9664\u79cd\u7c7b<\/p>\n<p><strong>*<\/strong> \u524d\u4e00\u4e2a\u5b57\u7b26 0 \u81f3\u65e0\u6570\u6b21\u62d3\u5c55<\/p>\n<p><strong> + <\/strong>\u8868\u793a\u524d\u4e00\u4e2a\u5b57\u7b261\u81f3\u65e0\u6570\u6b21\u62d3\u5c55<\/p>\n<p><strong>?<\/strong> \u524d\u4e00\u4e2a\u5b57\u7b260\u81f31\u6b21\u62d3\u5c55<\/p>\n<p><strong>abc|def<\/strong> \u8868\u793a\u4e8c\u8005\u4e4b\u95f4\u4efb\u610f\u4e00\u4e2a<\/p>\n<p><strong>{x}<\/strong>\u62d3\u5c55\u524d\u4e00\u4e2a\u5b57\u7b26x\u6b21<\/p>\n<p><strong>{m,n}<\/strong>\u62d3\u5c55\u524d\u4e00\u4e2a\u5b57\u7b26 m\u5230n\u6b21<\/p>\n<p><strong>^ab<\/strong> \u8868\u793a\u5b57\u7b26\u4e32\u5f00\u5934\u4e3aab<\/p>\n<p><strong>$cd<\/strong> \u8868\u793a\u4ee5 cd \u7ed3\u5c3e<\/p>\n<p>\\<strong>w<\/strong> \u8868\u793a\u6240\u6709\u5355\u8bcd \u7b49\u4ef7\u4e8e [A-Za-z0-9_]<\/p>\n<p><strong>\\d<\/strong>\u8868\u793a\u6240\u6709\u6570\u5b57[0-9]<\/p>\n<p><strong>PY{:3}ON<\/strong> \u8868\u793a\u524d\u4e00\u4e2a\u81ea\u7b26\u62d3\u5c55\u4e00\u6b21\u5230\u4e09\u6b21\u5373 PYON\u3001PYYON\u3001PYYYON<\/p>\n<h2>\u793a\u4f8b<\/h2>\n<p>^[A-Za-z0-9]+$ \u8868\u793a\u753126\u5b57\u6bcd\u548c\u6570\u5b57\u7ec4\u6210\u7684\u5b57\u7b26\u4e32<\/p>\n<p>^-?[0-9]+$ \u6216 ^-?\\d+$ \u8868\u793a\u6574\u6570<\/p>\n<p>^[0-9]* [1-9] *[0-9]$ \u8868\u793a\u6b63\u6574\u6570<\/p>\n<p>[\\u4e00- \\u9fa5] \u5339\u914d\u4e2d\u6587\u5b57\u7b26<\/p>\n<p>0-99 \uff1a[1-9]?\\d 100-199\uff1a1[0-9]{2}<\/p>\n<p>200-249\uff1a 2[0-4]\\d 250-255\uff1a 25[0-5]<\/p>\n<p>\u5339\u914dIPV4\u5730\u5740\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}$<\/code><\/pre>\n<p>\u5339\u914dIPV4\u5730\u5740(CIDR)\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}\/(([1-2]\\d)|(3[0-2])|(\\d))$<\/code><\/pre>\n<p>\u5339\u914d\u7a7a\u884c\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/^$\/gm<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u4f7f\u7528re\u5e93<\/h2>\n<p>\u200b <strong>re.search(\u8868\u8fbe\u5f0f , \u5f85\u5339\u914d\u5b57\u7b26\u4e32, \u63a7\u5236\u6807\u8bb0)<\/strong><\/p>\n<p>\u200b \u5b83\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6ee1\u8db3\u8868\u8fbe\u5f0f\u7684\u5b57\u7b26\uff0c\u5e76\u4e14\u4f1a\u8fd4\u56de match\u5bf9\u8c61\uff0c\u610f\u5473\u7740\u9700\u8981\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u8fdb\u884c\u64cd\u4f5c\uff0c\u82e5\u5b57\u7b26\u4e32\u6ca1\u6709\u5bf9\u5e94\u5185\u5bb9\uff0c\u5219\u4f1a\u8fd4\u56de None ,\u6240\u4ee5\u9700\u8981\u7528 if \u6765\u5224\u65ad\u662f\u5426\u6709\u5339\u914d\u503c<\/p>\n<p>\u5982<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">m = re.search(r'[1-9]\\d{5}', 'BIT 100081') \r\nif m:\r\n    print(m.group(0))<\/code><\/pre>\n<p>\u5373\u53ef\u6253\u5370\u51fa\u5339\u914d\u5b57\u7b26<\/p>\n<p>\u200b <strong>re.match(\u8868\u8fbe\u5f0f , \u5f85\u5339\u914d\u5b57\u7b26\u4e32, \u63a7\u5236\u6807\u8bb0)<\/strong><\/p>\n<p>\u200b \u5b83\u4f1a\u8fd4\u56de\u5b57\u7b26\u5f00\u5934\u7684\u5339\u914d\u4e14\u53ea\u5339\u914d\u5b57\u7b26\u5f00\u5934\uff0c\u5176\u4f59\u548c re.search \u4e00\u6837\uff0c\u8fd4\u56dematch\u5bf9\u8c61<\/p>\n<p>\u200b <strong>re.findall(\u8868\u8fbe\u5f0f , \u5f85\u5339\u914d\u5b57\u7b26\u4e32, \u63a7\u5236\u6807\u8bb0)<\/strong><\/p>\n<p>\u200b \u5b83\u4f1a\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7684\u5185\u5bb9\uff0c\u8fd4\u56de\u7b26\u5408\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u7ed3\u679c<\/p>\n<p>\u200b <strong>re.split(\u8868\u8fbe\u5f0f , \u5f85\u5339\u914d\u5b57\u7b26\u4e32, maxsplit=\u6700\u5927\u5206\u5272\u6570,\u63a7\u5236\u6807\u8bb0(flag))<\/strong><\/p>\n<p>\u200b \u5b83\u4f1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d\u7684\u5185\u5bb9\u53bb\u6389\uff0c\u7136\u540e\u5c06\u5206\u5272\u7ed3\u679c\u5206\u522b\u52a0\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u5b83\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u6700\u5927\u5206\u5272\u6570<\/p>\n<p>\u200b <strong>re.finditer(\u8868\u8fbe\u5f0f , \u5f85\u5339\u914d\u5b57\u7b26\u4e32, \u63a7\u5236\u6807\u8bb0)<\/strong><\/p>\n<p>\u200b \u5b83\u7684\u8fd4\u56de\u5185\u5bb9\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528for \u5faa\u73af\u6765\u8fed\u4ee3\u5355\u72ec\u8f93\u51fa\u5b57\u7b26\u4e32\u4e2d\u5339\u914d\u7684\u5185\u5bb9\uff0c\u6bcf\u4e2a\u8fed\u4ee3\u7684\u5143\u7d20\u662fmatch\u5bf9\u8c61<\/p>\n<p>\u200b <strong>re.sub(\u8868\u8fbe\u5f0f , \u66ff\u6362\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5b57\u7b26,\u5f85\u5339\u914d\u5b57\u7b26\u4e32,count=\u6700\u5927\u66ff\u6362\u6570\uff0c \u63a7\u5236\u6807\u8bb0)<\/strong><\/p>\n<p>\u200b \u5b83\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5339\u914d\u7684\u5185\u5bb9\u66ff\u6362\u6210\u7ed9\u5b9a\u5185\u5bb9<\/p>\n<h4>re\u7684 OOP\u7528\u6cd5<\/h4>\n<pre class=\"pure-highlightjs\"><code class=\"\">a = re.compile(r'[1-9]\\d{5}') \r\nb = a.search('BIT 100081')<\/code><\/pre>\n<p>\u53ef\u591a\u6b21\u4f7f\u7528 a \u5bf9\u8c61\uff0c\u6765\u52a0\u901f\u8fd0\u884c\u901f\u5ea6<\/p>\n<h4>\u8d2a\u5a6a\u5339\u914d\u548c\u6700\u5c0f\u5339\u914d<\/h4>\n<p>\u200b \u82e5\u5f85\u5339\u914d\u7684\u5b57\u7b26\u4e32\u4e2d\uff08\u540c\u4e00\u4e2a\u5355\u8bcd\uff09\u6709\u591a\u4e2a\u5339\u914d\u9879\uff0cre\u5e93\u9ed8\u8ba4\u8fd4\u56de\u6700\u957f\u7684\u90a3\u4e00\u4e2a\uff0c\u82e5\u9700\u8981\u6700\u5c0f\u5339\u914d\uff0c\u5219\u9700\u8981\u5728\u5339\u914d\u89c4\u5219\u4e2d\u52a0\u4e00\u4e2a ?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>RE\u57fa\u672c\u8bed\u6cd5 . \u8868\u793a\u4efb\u4f55\u4e00\u4e2a\u5b57\u7b26 [a-z]\u8868\u793a\u4efb\u4f55\u4e00\u4e2a\u5c0f\u5199\u5b57\u6bcd\u00a0 [A-Z] \u8868\u793a\u4efb\u4f55\u4e00\u4e2a\u5927\u5199\u5b57\u6bcd [^0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-computerscience"],"_links":{"self":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":7,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":594,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/32\/revisions\/594"}],"wp:attachment":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}