@@ -968,7 +968,7 @@ msgid ""
968968"Note that ``\\ B`` does not match an empty string, which differs from RE "
969969"implementations in other programming languages such as Perl. This behavior "
970970"is kept for compatibility reasons."
971- msgstr ""
971+ msgstr "请注意 `` \\ B`` 不会匹配空字符串,这与其他编程语言如 Perl 的 RE 实现不同。 此行为是出于兼容性考虑而保留的。 "
972972
973973#: ../../library/re.rst:583
974974msgid "``\\ d``"
@@ -1026,6 +1026,8 @@ msgid ""
10261026" other characters, for example the non-breaking spaces mandated by "
10271027"typography rules in many languages."
10281028msgstr ""
1029+ "匹配 Unicode 空白字符(如 :py:meth:`str.isspace` 所定义的)。 这包括 ``[ "
1030+ "\\ t\\ n\\ r\\ f\\ v]``,还包括许多其他字符,例如许多语言中由排版规则约定的非中断空白字符。"
10291031
10301032#: ../../library/re.rst:613
10311033msgid "Matches ``[ \\ t\\ n\\ r\\ f\\ v]`` if the :py:const:`~re.ASCII` flag is used."
@@ -2469,6 +2471,13 @@ msgid ""
24692471">>> displaymatch(valid.match(\" 727ak\" )) # Valid.\n"
24702472"\" <Match: '727ak', groups=()>\" "
24712473msgstr ""
2474+ ">>> valid = re.compile(r\" ^[a2-9tjqk]{5}$\" )\n"
2475+ ">>> displaymatch(valid.match(\" akt5q\" )) # 有效。\n"
2476+ "\" <Match: 'akt5q', groups=()>\" \n"
2477+ ">>> displaymatch(valid.match(\" akt5e\" )) # 无效。\n"
2478+ ">>> displaymatch(valid.match(\" akt\" )) # 无效。\n"
2479+ ">>> displaymatch(valid.match(\" 727ak\" )) # 有效。\n"
2480+ "\" <Match: '727ak', groups=()>\" "
24722481
24732482#: ../../library/re.rst:1607
24742483msgid ""
@@ -2486,6 +2495,12 @@ msgid ""
24862495">>> displaymatch(pair.match(\" 354aa\" )) # Pair of aces.\n"
24872496"\" <Match: '354aa', groups=('a',)>\" "
24882497msgstr ""
2498+ ">>> pair = re.compile(r\" .*(.).*\\ 1\" )\n"
2499+ ">>> displaymatch(pair.match(\" 717ak\" )) # 成对的 7。\n"
2500+ "\" <Match: '717', groups=('7',)>\" \n"
2501+ ">>> displaymatch(pair.match(\" 718ak\" )) # 没有成对。\n"
2502+ ">>> displaymatch(pair.match(\" 354aa\" )) # 成对的 a。\n"
2503+ "\" <Match: '354aa', groups=('a',)>\" "
24892504
24902505#: ../../library/re.rst:1617
24912506msgid ""
@@ -2509,6 +2524,19 @@ msgid ""
25092524">>> pair.match(\" 354aa\" ).group(1)\n"
25102525"'a'"
25112526msgstr ""
2527+ ">>> pair = re.compile(r\" .*(.).*\\ 1\" )\n"
2528+ ">>> pair.match(\" 717ak\" ).group(1)\n"
2529+ "'7'\n"
2530+ "\n"
2531+ "# 会报错因为 re.match() 返回 None,它没有 group() 方法。\n"
2532+ ">>> pair.match(\" 718ak\" ).group(1)\n"
2533+ "Traceback (most recent call last):\n"
2534+ " File \" <pyshell#23>\" , line 1, in <module>\n"
2535+ " re.match(r\" .*(.).*\\ 1\" , \" 718ak\" ).group(1)\n"
2536+ "AttributeError: 'NoneType' object has no attribute 'group'\n"
2537+ "\n"
2538+ ">>> pair.match(\" 354aa\" ).group(1)\n"
2539+ "'a'"
25122540
25132541#: ../../library/re.rst:1636
25142542msgid "Simulating scanf()"
@@ -2865,6 +2893,9 @@ msgid ""
28652893">>> re.findall(r\"\\ w+ly\\ b\" , text)\n"
28662894"['carefully', 'quickly']"
28672895msgstr ""
2896+ ">>> text = \" He was carefully disguised but captured quickly by police.\" \n"
2897+ ">>> re.findall(r\"\\ w+ly\\ b\" , text)\n"
2898+ "['carefully', 'quickly']"
28682899
28692900#: ../../library/re.rst:1817
28702901msgid "Finding all Adverbs and their Positions"
@@ -2890,6 +2921,11 @@ msgid ""
28902921"07-16: carefully\n"
28912922"40-47: quickly"
28922923msgstr ""
2924+ ">>> text = \" He was carefully disguised but captured quickly by police.\" \n"
2925+ ">>> for m in re.finditer(r\"\\ w+ly\\ b\" , text):\n"
2926+ "... print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))\n"
2927+ "07-16: carefully\n"
2928+ "40-47: quickly"
28932929
28942930#: ../../library/re.rst:1833
28952931msgid "Raw String Notation"
@@ -2912,6 +2948,10 @@ msgid ""
29122948">>> re.match(\"\\\\ W(.)\\\\ 1\\\\ W\" , \" ff \" )\n"
29132949"<re.Match object; span=(0, 4), match=' ff '>"
29142950msgstr ""
2951+ ">>> re.match(r\"\\ W(.)\\ 1\\ W\" , \" ff \" )\n"
2952+ "<re.Match object; span=(0, 4), match=' ff '>\n"
2953+ ">>> re.match(\"\\\\ W(.)\\\\ 1\\\\ W\" , \" ff \" )\n"
2954+ "<re.Match object; span=(0, 4), match=' ff '>"
29152955
29162956#: ../../library/re.rst:1845
29172957msgid ""
@@ -3007,6 +3047,57 @@ msgid ""
30073047"for token in tokenize(statements):\n"
30083048" print(token)"
30093049msgstr ""
3050+ "from typing import NamedTuple\n"
3051+ "import re\n"
3052+ "\n"
3053+ "class Token(NamedTuple):\n"
3054+ " type: str\n"
3055+ " value: str\n"
3056+ " line: int\n"
3057+ " column: int\n"
3058+ "\n"
3059+ "def tokenize(code):\n"
3060+ " keywords = {'IF', 'THEN', 'ENDIF', 'FOR', 'NEXT', 'GOSUB', 'RETURN'}\n"
3061+ " token_specification = [\n"
3062+ " ('NUMBER', r'\\ d+(\\ .\\ d*)?'), # 整数或小数\n"
3063+ " ('ASSIGN', r':='), # 赋值运算符\n"
3064+ " ('END', r';'), # 语句结束符\n"
3065+ " ('ID', r'[A-Za-z]+'), # 标识符\n"
3066+ " ('OP', r'[+\\ -*/]'), # 算术运算符\n"
3067+ " ('NEWLINE', r'\\ n'), # 行结束符\n"
3068+ " ('SKIP', r'[ \\ t]+'), # 跳过空格和制表符\n"
3069+ " ('MISMATCH', r'.'), # 任何其他字符\n"
3070+ " ]\n"
3071+ " tok_regex = '|'.join('(?P<%s>%s)' % pair for pair in token_specification)\n"
3072+ " line_num = 1\n"
3073+ " line_start = 0\n"
3074+ " for mo in re.finditer(tok_regex, code):\n"
3075+ " kind = mo.lastgroup\n"
3076+ " value = mo.group()\n"
3077+ " column = mo.start() - line_start\n"
3078+ " if kind == 'NUMBER':\n"
3079+ " value = float(value) if '.' in value else int(value)\n"
3080+ " elif kind == 'ID' and value in keywords:\n"
3081+ " kind = value\n"
3082+ " elif kind == 'NEWLINE':\n"
3083+ " line_start = mo.end()\n"
3084+ " line_num += 1\n"
3085+ " continue\n"
3086+ " elif kind == 'SKIP':\n"
3087+ " continue\n"
3088+ " elif kind == 'MISMATCH':\n"
3089+ " raise RuntimeError(f'{value!r} unexpected on line {line_num}')\n"
3090+ " yield Token(kind, value, line_num, column)\n"
3091+ "\n"
3092+ "statements = '''\n"
3093+ " IF quantity THEN\n"
3094+ " total := total + price * quantity;\n"
3095+ " tax := price * 0.05;\n"
3096+ " ENDIF;\n"
3097+ "'''\n"
3098+ "\n"
3099+ "for token in tokenize(statements):\n"
3100+ " print(token)"
30103101
30113102#: ../../library/re.rst:1919
30123103msgid "The tokenizer produces the following output::"
@@ -3034,6 +3125,25 @@ msgid ""
30343125"Token(type='ENDIF', value='ENDIF', line=5, column=4)\n"
30353126"Token(type='END', value=';', line=5, column=9)"
30363127msgstr ""
3128+ "Token(type='IF', value='IF', line=2, column=4)\n"
3129+ "Token(type='ID', value='quantity', line=2, column=7)\n"
3130+ "Token(type='THEN', value='THEN', line=2, column=16)\n"
3131+ "Token(type='ID', value='total', line=3, column=8)\n"
3132+ "Token(type='ASSIGN', value=':=', line=3, column=14)\n"
3133+ "Token(type='ID', value='total', line=3, column=17)\n"
3134+ "Token(type='OP', value='+', line=3, column=23)\n"
3135+ "Token(type='ID', value='price', line=3, column=25)\n"
3136+ "Token(type='OP', value='*', line=3, column=31)\n"
3137+ "Token(type='ID', value='quantity', line=3, column=33)\n"
3138+ "Token(type='END', value=';', line=3, column=41)\n"
3139+ "Token(type='ID', value='tax', line=4, column=8)\n"
3140+ "Token(type='ASSIGN', value=':=', line=4, column=12)\n"
3141+ "Token(type='ID', value='price', line=4, column=15)\n"
3142+ "Token(type='OP', value='*', line=4, column=21)\n"
3143+ "Token(type='NUMBER', value=0.05, line=4, column=23)\n"
3144+ "Token(type='END', value=';', line=4, column=27)\n"
3145+ "Token(type='ENDIF', value='ENDIF', line=5, column=4)\n"
3146+ "Token(type='END', value=';', line=5, column=9)"
30373147
30383148#: ../../library/re.rst:1942
30393149msgid ""
0 commit comments