{"id":167429,"date":"2019-12-31T13:00:55","date_gmt":"2019-12-31T05:00:55","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=167429"},"modified":"2019-12-23T10:57:37","modified_gmt":"2019-12-23T02:57:37","slug":"retrieve-and-replace","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/retrieve-and-replace.html","title":{"rendered":"Python3 \u6b63\u5219\u8868\u8fbe\u5f0f\u2014\u68c0\u7d22\u548c\u66ff\u6362"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nPython \u7684re\u6a21\u5757\u63d0\u4f9b\u4e86re.sub\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5339\u914d\u9879\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u8bed\u6cd5\uff1a<\/p>\n

re.sub(pattern, repl, string, count=0, flags=0)<\/pre>\n

\u53c2\u6570\uff1a<\/p>\n\n\n\n\n\n\n
pattern : \u6b63\u5219\u4e2d\u7684\u6a21\u5f0f\u5b57\u7b26\u4e32<\/td>\n\n
repl : \u66ff\u6362\u7684\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4e3a\u4e00\u4e2a\u51fd\u6570<\/td>\n\n
string : \u8981\u88ab\u67e5\u627e\u66ff\u6362\u7684\u539f\u59cb\u5b57\u7b26\u4e32<\/td>\n\n
count : \u6a21\u5f0f\u5339\u914d\u540e\u66ff\u6362\u7684\u6700\u5927\u6b21\u6570\uff0c\u9ed8\u8ba4 0 \u8868\u793a\u66ff\u6362\u6240\u6709\u7684\u5339\u914d<\/td>\n\n
flags : \u7f16\u8bd1\u65f6\u7528\u7684\u5339\u914d\u6a21\u5f0f\uff0c\u6570\u5b57\u5f62\u5f0f<\/td>\n\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/table>\n

\u524d\u4e09\u4e2a\u4e3a\u5fc5\u9009\u53c2\u6570\uff0c\u540e\u4e24\u4e2a\u4e3a\u53ef\u9009\u53c2\u6570\u3002<\/p>\n

\u5b9e\u4f8b<\/p>\n

#!\/usr\/bin\/python3\r\nimport re\r\n\r\nphone = \"2004-959-559 # \u8fd9\u662f\u4e00\u4e2a\u7535\u8bdd\u53f7\u7801\"\r\n\r\n# \u5220\u9664\u6ce8\u91ca\r\nnum = re.sub(r'#.*$', \"\", phone)\r\nprint (\"\u7535\u8bdd\u53f7\u7801 : \", num)\r\n\r\n# \u79fb\u9664\u975e\u6570\u5b57\u7684\u5185\u5bb9\r\nnum = re.sub(r'\\D', \"\", phone)\r\nprint (\"\u7535\u8bdd\u53f7\u7801 : \", num)<\/pre>\n

\u4ee5\u4e0a\u5b9e\u4f8b\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

\u7535\u8bdd\u53f7\u7801 : 2004-959-559\r\n\u7535\u8bdd\u53f7\u7801 : 2004959559<\/pre>\n
repl \u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570<\/strong><\/div>\n

\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5339\u914d\u7684\u6570\u5b57\u4e58\u4e8e 2\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

#!\/usr\/bin\/python\r\n\r\nimport re\r\n\r\n# \u5c06\u5339\u914d\u7684\u6570\u5b57\u4e58\u4e8e 2\r\ndef double(matched):\r\nvalue = int(matched.group('value'))\r\nreturn str(value * 2)\r\n\r\ns = 'A23G4HFD567'\r\nprint(re.sub('(?P\\d+)', double, s))<\/pre>\n

\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

A46G8HFD1134\r\ncompile \u51fd\u6570\r\ncompile \u51fd\u6570\u7528\u4e8e\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u751f\u6210\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff08 Pattern \uff09\u5bf9\u8c61\uff0c\u4f9b match() \u548c search() \u8fd9\u4e24\u4e2a\u51fd\u6570\u4f7f\u7528\u3002<\/pre>\n

\u8bed\u6cd5\u683c\u5f0f\u4e3a\uff1a<\/p>\n

re.compile(pattern[, flags])<\/pre>\n

\u53c2\u6570\uff1a<\/p>\n

pattern : \u4e00\u4e2a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n

flags \u53ef\u9009\uff0c\u8868\u793a\u5339\u914d\u6a21\u5f0f\uff0c\u6bd4\u5982\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u591a\u884c\u6a21\u5f0f\u7b49\uff0c\u5177\u4f53\u53c2\u6570\u4e3a\uff1a
\nre.I \u5ffd\u7565\u5927\u5c0f\u5199
\nre.L \u8868\u793a\u7279\u6b8a\u5b57\u7b26\u96c6 \\w, \\W, \\b, \\B, \\s, \\S \u4f9d\u8d56\u4e8e\u5f53\u524d\u73af\u5883<\/p>\n

re.M \u591a\u884c\u6a21\u5f0f<\/p>\n

re.S \u5373\u4e3a' . '\u5e76\u4e14\u5305\u62ec\u6362\u884c\u7b26\u5728\u5185\u7684\u4efb\u610f\u5b57\u7b26\uff08' . '\u4e0d\u5305\u62ec\u6362\u884c\u7b26\uff09<\/p>\n

re.U \u8868\u793a\u7279\u6b8a\u5b57\u7b26\u96c6 \\w, \\W, \\b, \\B, \\d, \\D, \\s, \\S \u4f9d\u8d56\u4e8e Unicode \u5b57\u7b26\u5c5e\u6027\u6570\u636e\u5e93<\/p>\n

re.X \u4e3a\u4e86\u589e\u52a0\u53ef\u8bfb\u6027\uff0c\u5ffd\u7565\u7a7a\u683c\u548c' # '\u540e\u9762\u7684\u6ce8\u91ca
\n\u5b9e\u4f8b<\/strong><\/p>\n

\u5b9e\u4f8b\r\n>>>import re\r\n>>> pattern = re.compile(r'\\d+') # \u7528\u4e8e\u5339\u914d\u81f3\u5c11\u4e00\u4e2a\u6570\u5b57\r\n>>> m = pattern.match('one12twothree34four') # \u67e5\u627e\u5934\u90e8\uff0c\u6ca1\u6709\u5339\u914d\r\n>>> print m\r\nNone\r\n>>> m = pattern.match('one12twothree34four', 2, 10) # \u4ece'e'\u7684\u4f4d\u7f6e\u5f00\u59cb\u5339\u914d\uff0c\u6ca1\u6709\u5339\u914d\r\n>>> print m\r\nNone\r\n>>> m = pattern.match('one12twothree34four', 3, 10) # \u4ece'1'\u7684\u4f4d\u7f6e\u5f00\u59cb\u5339\u914d\uff0c\u6b63\u597d\u5339\u914d\r\n>>> print m # \u8fd4\u56de\u4e00\u4e2a Match \u5bf9\u8c61\r\n<_sre.SRE_Match object at 0x10a42aac0>\r\n>>> m.group(0) # \u53ef\u7701\u7565 0\r\n'12'\r\n>>> m.start(0) # \u53ef\u7701\u7565 0\r\n3\r\n>>> m.end(0) # \u53ef\u7701\u7565 0\r\n5\r\n>>> m.span(0) # \u53ef\u7701\u7565 0\r\n(3, 5)<\/pre>\n

\u5728\u4e0a\u9762\uff0c\u5f53\u5339\u914d\u6210\u529f\u65f6\u8fd4\u56de\u4e00\u4e2a Match \u5bf9\u8c61\uff0c\u5176\u4e2d\uff1a<\/p>\n

group([group1, \u2026]) \u65b9\u6cd5\u7528\u4e8e\u83b7\u5f97\u4e00\u4e2a\u6216\u591a\u4e2a\u5206\u7ec4\u5339\u914d\u7684\u5b57\u7b26\u4e32\uff0c\u5f53\u8981\u83b7\u5f97\u6574\u4e2a\u5339\u914d\u7684\u5b50\u4e32\u65f6\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528 group() \u6216 group(0)\uff1b
\nstart([group]) \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u5206\u7ec4\u5339\u914d\u7684\u5b50\u4e32\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e\uff08\u5b50\u4e32\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u7d22\u5f15\uff09\uff0c\u53c2\u6570\u9ed8\u8ba4\u503c\u4e3a 0\uff1b
\nend([group]) \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u5206\u7ec4\u5339\u914d\u7684\u5b50\u4e32\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u7ed3\u675f\u4f4d\u7f6e\uff08\u5b50\u4e32\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u7d22\u5f15+1\uff09\uff0c\u53c2\u6570\u9ed8\u8ba4\u503c\u4e3a 0\uff1b
\nspan([group]) \u65b9\u6cd5\u8fd4\u56de (start(group), end(group))\u3002
\n\u518d\u770b\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n

\u5b9e\u4f8b\r\n>>>import re\r\n>>> pattern = re.compile(r'([a-z]+) ([a-z]+)', re.I) # re.I \u8868\u793a\u5ffd\u7565\u5927\u5c0f\u5199\r\n>>> m = pattern.match('Hello World Wide Web')\r\n>>> print m # \u5339\u914d\u6210\u529f\uff0c\u8fd4\u56de\u4e00\u4e2a Match \u5bf9\u8c61\r\n<_sre.SRE_Match object at 0x10bea83e8>\r\n>>> m.group(0) # \u8fd4\u56de\u5339\u914d\u6210\u529f\u7684\u6574\u4e2a\u5b50\u4e32\r\n'Hello World'\r\n>>> m.span(0) # \u8fd4\u56de\u5339\u914d\u6210\u529f\u7684\u6574\u4e2a\u5b50\u4e32\u7684\u7d22\u5f15\r\n(0, 11)\r\n>>> m.group(1) # \u8fd4\u56de\u7b2c\u4e00\u4e2a\u5206\u7ec4\u5339\u914d\u6210\u529f\u7684\u5b50\u4e32\r\n'Hello'\r\n>>> m.span(1) # \u8fd4\u56de\u7b2c\u4e00\u4e2a\u5206\u7ec4\u5339\u914d\u6210\u529f\u7684\u5b50\u4e32\u7684\u7d22\u5f15\r\n(0, 5)\r\n>>> m.group(2) # \u8fd4\u56de\u7b2c\u4e8c\u4e2a\u5206\u7ec4\u5339\u914d\u6210\u529f\u7684\u5b50\u4e32\r\n'World'\r\n>>> m.span(2) # \u8fd4\u56de\u7b2c\u4e8c\u4e2a\u5206\u7ec4\u5339\u914d\u6210\u529f\u7684\u5b50\u4e32\u7d22\u5f15\r\n(6, 11)\r\n>>> m.groups() # \u7b49\u4ef7\u4e8e (m.group(1), m.group(2), ...)\r\n('Hello', 'World')\r\n>>> m.group(3) # \u4e0d\u5b58\u5728\u7b2c\u4e09\u4e2a\u5206\u7ec4\r\nTraceback (most recent call last):\r\nFile \"\", line 1, in\r\nIndexError: no such group<\/pre>\n
findall<\/strong><\/div>\n

\u5728\u5b57\u7b26\u4e32\u4e2d\u627e\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u6240\u5339\u914d\u7684\u6240\u6709\u5b50\u4e32\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\uff0c\u5219\u8fd4\u56de\u7a7a\u5217\u8868\u3002<\/p>\n

\u6ce8\u610f\uff1a match \u548c search \u662f\u5339\u914d\u4e00\u6b21 findall \u5339\u914d\u6240\u6709\u3002<\/p>\n

\u8bed\u6cd5\u683c\u5f0f\u4e3a\uff1a<\/p>\n

re.findall(string[, pos[, endpos]])<\/pre>\n

\u53c2\u6570\uff1a<\/p>\n\n\n\n\n\n\n
pattern : \u6b63\u5219\u4e2d\u7684\u6a21\u5f0f\u5b57\u7b26\u4e32<\/td>\n\n
repl : \u66ff\u6362\u7684\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4e3a\u4e00\u4e2a\u51fd\u6570<\/td>\n\n
string : \u8981\u88ab\u67e5\u627e\u66ff\u6362\u7684\u539f\u59cb\u5b57\u7b26\u4e32<\/td>\n\n
count : \u6a21\u5f0f\u5339\u914d\u540e\u66ff\u6362\u7684\u6700\u5927\u6b21\u6570\uff0c\u9ed8\u8ba4 0 \u8868\u793a\u66ff\u6362\u6240\u6709\u7684\u5339\u914d<\/td>\n\n
flags : \u7f16\u8bd1\u65f6\u7528\u7684\u5339\u914d\u6a21\u5f0f\uff0c\u6570\u5b57\u5f62\u5f0f<\/td>\n\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/table>\n

string \u5f85\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002
\npos \u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u9ed8\u8ba4\u4e3a 0\u3002
\nendpos \u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u4f4d\u7f6e\uff0c\u9ed8\u8ba4\u4e3a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002
\n\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\uff1a<\/p>\n

\u5b9e\u4f8b\r\nimport re\r\n\r\npattern = re.compile(r'\\d+') # \u67e5\u627e\u6570\u5b57\r\nresult1 = pattern.findall('runoob 123 google 456')\r\nresult2 = pattern.findall('run88oob123google456', 0, 10)\r\n\r\nprint(result1)\r\nprint(result2)<\/pre>\n

\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n

['123', '456']\r\n['88', '12']<\/pre>\n
re.finditer<\/strong><\/div>\n

\u548c findall \u7c7b\u4f3c\uff0c\u5728\u5b57\u7b26\u4e32\u4e2d\u627e\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u6240\u5339\u914d\u7684\u6240\u6709\u5b50\u4e32\uff0c\u5e76\u628a\u5b83\u4eec\u4f5c\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u5668\u8fd4\u56de\u3002<\/p>\n

re.finditer(pattern, string, flags=0)<\/pre>\n

\u53c2\u6570\uff1a<\/strong><\/p>\n\n\n\n\n\n
\u53c2\u6570<\/td>\n\u63cf\u8ff0<\/td>\n\n
pattern<\/td>\n\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/td>\n\n
string<\/td>\n\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/td>\n\n
flags<\/td>\n\u6807\u5fd7\u4f4d\uff0c\u7528\u4e8e\u63a7\u5236\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u65b9\u5f0f\uff0c\u5982\uff1a\u662f\u5426\u533a\u5206\u5927\u5c0f\u5199\uff0c\u591a\u884c\u5339\u914d\u7b49\u7b49\u3002\u53c2\u89c1\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u4fee\u9970\u7b26 - \u53ef\u9009\u6807\u5fd7<\/td>\n\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/table>\n
\u5b9e\u4f8b\r\nimport re\r\n\r\nit = re.finditer(r\"\\d+\",\"12a32bc43jf3\")\r\nfor match in it:\r\nprint (match.group() )<\/pre>\n

\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n

12\r\n32\r\n43\r\n3<\/pre>\n
re.split<\/strong><\/div>\n

split \u65b9\u6cd5\u6309\u7167\u80fd\u591f\u5339\u914d\u7684\u5b50\u4e32\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u540e\u8fd4\u56de\u5217\u8868\uff0c\u5b83\u7684\u4f7f\u7528\u5f62\u5f0f\u5982\u4e0b\uff1a<\/p>\n

re.split(pattern, string[, maxsplit=0, flags=0])<\/pre>\n

\u53c2\u6570\uff1a<\/strong><\/p>\n\n\n\n\n\n\n
\u53c2\u6570<\/td>\n\u63cf\u8ff0<\/td>\n\n
pattern<\/td>\n\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/td>\n\n
string<\/td>\n\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32<\/td>\n\n
maxsplit<\/td>\n\u5206\u9694\u6b21\u6570\uff0cmaxsplit=1 \u5206\u9694\u4e00\u6b21\uff0c\u9ed8\u8ba4\u4e3a 0\uff0c\u4e0d\u9650\u5236\u6b21\u6570<\/td>\n\n
flags<\/td>\n\u6807\u5fd7\u4f4d\uff0c\u7528\u4e8e\u63a7\u5236\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u65b9\u5f0f\uff0c\u5982\uff1a\u662f\u5426\u533a\u5206\u5927\u5c0f\u5199\uff0c\u591a\u884c\u5339\u914d\u7b49\u7b49\u3002\u53c2\u89c1\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u4fee\u9970\u7b26 - \u53ef\u9009\u6807\u5fd7<\/td>\n\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/td>\n<\/tr>\n<\/table>\n
\u5b9e\u4f8b\r\n>>>import re\r\n>>> re.split('\\W+', 'runoob, runoob, runoob.')\r\n['runoob', 'runoob', 'runoob', '']\r\n>>> re.split('(\\W+)', ' runoob, runoob, runoob.')\r\n['', ' ', 'runoob', ', ', 'runoob', ', ', 'runoob', '.', '']\r\n>>> re.split('\\W+', ' runoob, runoob, runoob.', 1)\r\n['', 'runoob, runoob, runoob.']\r\n\r\n>>> re.split('a*', 'hello world') # \u5bf9\u4e8e\u4e00\u4e2a\u627e\u4e0d\u5230\u5339\u914d\u7684\u5b57\u7b26\u4e32\u800c\u8a00\uff0csplit \u4e0d\u4f1a\u5bf9\u5176\u4f5c\u51fa\u5206\u5272\r\n['hello world']<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u8bed\u6cd5\uff1a re.sub(pattern, repl, string, count=0, flags=0) \u53c2\u6570\uff1a […]<\/p>\n","protected":false},"author":321,"featured_media":167427,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-167429","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=167429"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167429\/revisions"}],"predecessor-version":[{"id":167432,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167429\/revisions\/167432"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/167427"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=167429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=167429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=167429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}