# SOME DESCRIPTIVE TITLE.
# Copyright (C) 2001 Python Software Foundation
# This file is distributed under the same license as the Python package.
# FIRST AUTHOR , YEAR.
#
# Translators:
# python-doc bot, 2026
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Python 3.15\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2026-08-09 14:27+0000\n"
"PO-Revision-Date: 2025-09-16 00:00+0000\n"
"Last-Translator: python-doc bot, 2026\n"
"Language-Team: Polish (https://app.transifex.com/python-doc/teams/5390/pl/)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: pl\n"
"Plural-Forms: nplurals=4; plural=(n==1 ? 0 : (n%10>=2 && n%10=0 && n%10=5 && "
"n%10=12 && n%100>> import re\n"
">>> p = re.compile('ab*')\n"
">>> p\n"
"re.compile('ab*')"
msgstr ""
">>> import re\n"
">>> p = re.compile('ab*')\n"
">>> p\n"
"re.compile('ab*')"
msgid ""
":func:`re.compile` also accepts an optional *flags* argument, used to enable "
"various special features and syntax variations. We'll go over the available "
"settings later, but for now a single example will do::"
msgstr ""
msgid ">>> p = re.compile('ab*', re.IGNORECASE)"
msgstr ">>> p = re.compile('ab*', re.IGNORECASE)"
msgid ""
"The RE is passed to :func:`re.compile` as a string. REs are handled as "
"strings because regular expressions aren't part of the core Python language, "
"and no special syntax was created for expressing them. (There are "
"applications that don't need REs at all, so there's no need to bloat the "
"language specification by including them.) Instead, the :mod:`re` module is "
"simply a C extension module included with Python, just like the :mod:"
"`socket` or :mod:`zlib` modules."
msgstr ""
msgid ""
"Putting REs in strings keeps the Python language simpler, but has one "
"disadvantage which is the topic of the next section."
msgstr ""
msgid "The backslash plague"
msgstr ""
msgid ""
"As stated earlier, regular expressions use the backslash character "
"(``'\\'``) to indicate special forms or to allow special characters to be "
"used without invoking their special meaning. This conflicts with Python's "
"usage of the same character for the same purpose in string literals."
msgstr ""
msgid ""
"Let's say you want to write a RE that matches the string ``\\section``, "
"which might be found in a LaTeX file. To figure out what to write in the "
"program code, start with the desired string to be matched. Next, you must "
"escape any backslashes and other metacharacters by preceding them with a "
"backslash, resulting in the string ``\\\\section``. The resulting string "
"that must be passed to :func:`re.compile` must be ``\\\\section``. However, "
"to express this as a Python string literal, both backslashes must be escaped "
"*again*."
msgstr ""
msgid "Characters"
msgstr ""
msgid "Stage"
msgstr ""
msgid "``\\section``"
msgstr "``\\section``"
msgid "Text string to be matched"
msgstr ""
msgid "``\\\\section``"
msgstr "``\\\\section``"
msgid "Escaped backslash for :func:`re.compile`"
msgstr ""
msgid "``\"\\\\\\\\section\"``"
msgstr "``\"\\\\\\\\section\"``"
msgid "Escaped backslashes for a string literal"
msgstr ""
msgid ""
"In short, to match a literal backslash, one has to write ``'\\\\\\\\'`` as "
"the RE string, because the regular expression must be ``\\\\``, and each "
"backslash must be expressed as ``\\\\`` inside a regular Python string "
"literal. In REs that feature backslashes repeatedly, this leads to lots of "
"repeated backslashes and makes the resulting strings difficult to understand."
msgstr ""
msgid ""
"The solution is to use Python's raw string notation for regular expressions; "
"backslashes are not handled in any special way in a string literal prefixed "
"with ``'r'``, so ``r\"\\n\"`` is a two-character string containing ``'\\'`` "
"and ``'n'``, while ``\"\\n\"`` is a one-character string containing a "
"newline. Regular expressions will often be written in Python code using this "
"raw string notation."
msgstr ""
msgid ""
"In addition, special escape sequences that are valid in regular expressions, "
"but not valid as Python string literals, now result in a :exc:"
"`SyntaxWarning` and will eventually become a :exc:`SyntaxError`, which means "
"the sequences will be invalid if raw string notation or escaping the "
"backslashes isn't used."
msgstr ""
msgid "Regular String"
msgstr ""
msgid "Raw string"
msgstr ""
msgid "``\"ab*\"``"
msgstr "``\"ab*\"``"
msgid "``r\"ab*\"``"
msgstr "``r\"ab*\"``"
msgid "``r\"\\\\section\"``"
msgstr "``r\"\\\\section\"``"
msgid "``\"\\\\w+\\\\s+\\\\1\"``"
msgstr "``\"\\\\w+\\\\s+\\\\1\"``"
msgid "``r\"\\w+\\s+\\1\"``"
msgstr "``r\"\\w+\\s+\\1\"``"
msgid "Performing matches"
msgstr ""
msgid ""
"Once you have an object representing a compiled regular expression, what do "
"you do with it? Pattern objects have several methods and attributes. Only "
"the most significant ones will be covered here; consult the :mod:`re` docs "
"for a complete listing."
msgstr ""
msgid "Method/Attribute"
msgstr "Metoda/atrybut"
msgid "Purpose"
msgstr ""
msgid "``search()``"
msgstr "``search()``"
msgid "Scan through a string, looking for any location where this RE matches."
msgstr ""
msgid "``prefixmatch()``"
msgstr ""
msgid ""
"Determine if the RE matches at the beginning of the string. Previously "
"named :ref:`match() `."
msgstr ""
msgid "``findall()``"
msgstr "``findall()``"
msgid "Find all substrings where the RE matches, and return them as a list."
msgstr ""
msgid "``finditer()``"
msgstr "``finditer()``"
msgid ""
"Find all substrings where the RE matches, and return them as an :term:"
"`iterator`."
msgstr ""
msgid ""
":meth:`~re.Pattern.search` and :meth:`~re.Pattern.prefixmatch` return "
"``None`` if no match can be found. If they're successful, a :ref:`match "
"object ` instance is returned, containing information about "
"the match: where it starts and ends, the substring it matched, and more."
msgstr ""
msgid ""
"You can learn about this by interactively experimenting with the :mod:`re` "
"module."
msgstr ""
msgid ""
"This HOWTO uses the standard Python interpreter for its examples. First, run "
"the Python interpreter, import the :mod:`re` module, and compile a RE::"
msgstr ""
msgid ""
">>> import re\n"
">>> p = re.compile('[a-z]+')\n"
">>> p\n"
"re.compile('[a-z]+')"
msgstr ""
">>> import re\n"
">>> p = re.compile('[a-z]+')\n"
">>> p\n"
"re.compile('[a-z]+')"
msgid ""
"Now, you can try matching various strings against the RE ``[a-z]+``. An "
"empty string shouldn't match at all, since ``+`` means 'one or more "
"repetitions'. :meth:`~re.Pattern.search` should return ``None`` in this "
"case, which will cause the interpreter to print no output. You can "
"explicitly print the result of :meth:`!search` to make this clear. ::"
msgstr ""
msgid ""
">>> p.search(\"\")\n"
">>> print(p.search(\"\"))\n"
"None"
msgstr ""
msgid ""
"Now, let's try it on a string that it should match, such as ``tempo``. In "
"this case, :meth:`~re.Pattern.search` will return a :ref:`match object "
"`, so you should store the result in a variable for later "
"use. ::"
msgstr ""
msgid ""
">>> m = p.search('tempo')\n"
">>> m\n"
""
msgstr ""
msgid ""
"Now you can query the :ref:`match object ` for information "
"about the matching string. Match object instances also have several methods "
"and attributes; the most important ones are:"
msgstr ""
msgid "``group()``"
msgstr "``group()``"
msgid "Return the string matched by the RE"
msgstr ""
msgid "``start()``"
msgstr "``start()``"
msgid "Return the starting position of the match"
msgstr ""
msgid "``end()``"
msgstr "``end()``"
msgid "Return the ending position of the match"
msgstr ""
msgid "``span()``"
msgstr "``span()``"
msgid "Return a tuple containing the (start, end) positions of the match"
msgstr ""
msgid "Trying these methods will soon clarify their meaning::"
msgstr ""
msgid ""
">>> m.group()\n"
"'tempo'\n"
">>> m.start(), m.end()\n"
"(0, 5)\n"
">>> m.span()\n"
"(0, 5)"
msgstr ""
">>> m.group()\n"
"'tempo'\n"
">>> m.start(), m.end()\n"
"(0, 5)\n"
">>> m.span()\n"
"(0, 5)"
msgid ""
":meth:`~re.Match.group` returns the substring that was matched by the RE. :"
"meth:`~re.Match.start` and :meth:`~re.Match.end` return the starting and "
"ending index of the match. :meth:`~re.Match.span` returns both start and end "
"indexes in a single tuple. The :meth:`~re.Pattern.search` method of patterns "
"scans through the string, so the match may not start at zero. However, the :"
"meth:`~re.Pattern.prefixmatch` method only checks if the RE matches at the "
"start of a string, so :meth:`!start` will always be zero in that case. ::"
msgstr ""
msgid ""
">>> m = p.search('::: message'); print(m)\n"
"\n"
">>> m.group()\n"
"'message'\n"
">>> m.span()\n"
"(4, 11)\n"
">>> print(p.prefixmatch('::: message'))\n"
"None"
msgstr ""
msgid ""
"In actual programs, the most common style is to store the :ref:`match object "
"` in a variable, and then check if it was ``None``. This "
"usually looks like::"
msgstr ""
msgid ""
"p = re.compile( ... )\n"
"m = p.search( 'string goes here' )\n"
"if m:\n"
" print('Match found: ', m.group())\n"
"else:\n"
" print('No match')"
msgstr ""
msgid ""
"Two pattern methods return all of the matches for a pattern. :meth:`~re."
"Pattern.findall` returns a list of matching strings::"
msgstr ""
msgid ""
">>> p = re.compile(r'\\d+')\n"
">>> p.findall('12 drummers drumming, 11 pipers piping, 10 lords a-leaping')\n"
"['12', '11', '10']"
msgstr ""
msgid ""
"The ``r`` prefix, making the literal a raw string literal, is needed in this "
"example because escape sequences in a normal \"cooked\" string literal that "
"are not recognized by Python, as opposed to regular expressions, now result "
"in a :exc:`SyntaxWarning` and will eventually become a :exc:`SyntaxError`. "
"See :ref:`the-backslash-plague`."
msgstr ""
msgid ""
":meth:`~re.Pattern.findall` has to create the entire list before it can be "
"returned as the result. The :meth:`~re.Pattern.finditer` method returns a "
"sequence of :ref:`match object ` instances as an :term:"
"`iterator`::"
msgstr ""
msgid ""
">>> iterator = p.finditer('12 drummers drumming, 11 ... 10 ...')\n"
">>> iterator\n"
"\n"
">>> for match in iterator:\n"
"... print(match.span())\n"
"...\n"
"(0, 2)\n"
"(22, 24)\n"
"(29, 31)"
msgstr ""
msgid "Module-level functions"
msgstr ""
msgid ""
"You don't have to create a pattern object and call its methods; the :mod:"
"`re` module also provides top-level functions called :func:`~re.search`, :"
"func:`~re.prefixmatch`, :func:`~re.findall`, :func:`~re.sub`, and so forth. "
"These functions take the same arguments as the corresponding pattern method "
"with the RE string added as the first argument, and still return either "
"``None`` or a :ref:`match object ` instance. ::"
msgstr ""
msgid ""
">>> print(re.prefixmatch(r'From\\s+', 'Fromage amk'))\n"
"None\n"
">>> re.prefixmatch(r'From\\s+', 'From amk Thu May 14 19:12:10 1998')\n"
""
msgstr ""
msgid ""
"Under the hood, these functions simply create a pattern object for you and "
"call the appropriate method on it. They also store the compiled object in a "
"cache, so future calls using the same RE won't need to parse the pattern "
"again and again."
msgstr ""
msgid ""
"Should you use these module-level functions, or should you get the pattern "
"and call its methods yourself? If you're accessing a regex within a loop, "
"pre-compiling it will save a few function calls. Outside of loops, there's "
"not much difference thanks to the internal cache."
msgstr ""
msgid "Compilation flags"
msgstr ""
msgid ""
"Compilation flags let you modify some aspects of how regular expressions "
"work. Flags are available in the :mod:`re` module under two names, a long "
"name such as :const:`IGNORECASE` and a short, one-letter form such as :const:"
"`I`. (If you're familiar with Perl's pattern modifiers, the one-letter "
"forms use the same letters; the short form of :const:`re.VERBOSE` is :const:"
"`re.X`, for example.) Multiple flags can be specified by bitwise OR-ing "
"them; ``re.I | re.M`` sets both the :const:`I` and :const:`M` flags, for "
"example."
msgstr ""
msgid ""
"Here's a table of the available flags, followed by a more detailed "
"explanation of each one."
msgstr ""
msgid "Flag"
msgstr ""
msgid "Meaning"
msgstr "Znaczenie"
msgid ":const:`ASCII`, :const:`A`"
msgstr ":const:`ASCII`, :const:`A`"
msgid ""
"Makes several escapes like ``\\w``, ``\\b``, ``\\s`` and ``\\d`` match only "
"on ASCII characters with the respective property."
msgstr ""
msgid ":const:`DOTALL`, :const:`S`"
msgstr ":const:`DOTALL`, :const:`S`"
msgid "Make ``.`` match any character, including newlines."
msgstr ""
msgid ":const:`IGNORECASE`, :const:`I`"
msgstr ":const:`IGNORECASE`, :const:`I`"
msgid "Do case-insensitive matches."
msgstr ""
msgid ":const:`LOCALE`, :const:`L`"
msgstr ":const:`LOCALE`, :const:`L`"
msgid "Do a locale-aware match."
msgstr ""
msgid ":const:`MULTILINE`, :const:`M`"
msgstr ":const:`MULTILINE`, :const:`M`"
msgid "Multi-line matching, affecting ``^`` and ``$``."
msgstr "Multi-line matching, affecting ``^`` and ``$``."
msgid ":const:`VERBOSE`, :const:`X` (for 'extended')"
msgstr ""
msgid ""
"Enable verbose REs, which can be organized more cleanly and understandably."
msgstr ""
msgid ""
"Perform case-insensitive matching; character class and literal strings will "
"match letters by ignoring case. For example, ``[A-Z]`` will match lowercase "
"letters, too. Full Unicode matching also works unless the :const:`ASCII` "
"flag is used to disable non-ASCII matches. When the Unicode patterns ``[a-"
"z]`` or ``[A-Z]`` are used in combination with the :const:`IGNORECASE` flag, "
"they will match the 52 ASCII letters and 4 additional non-ASCII letters: "
"'' (U+0130, Latin capital letter I with dot above), '' (U+0131, Latin "
"small letter dotless i), '' (U+017F, Latin small letter long s) and "
"'' (U+212A, Kelvin sign). ``Spam`` will match ``'Spam'``, ``'spam'``, "
"``'spAM'``, or ``'pam'`` (the latter is matched only in Unicode mode). This "
"lowercasing doesn't take the current locale into account; it will if you "
"also set the :const:`LOCALE` flag."
msgstr ""
msgid ""
"Make ``\\w``, ``\\W``, ``\\b``, ``\\B`` and case-insensitive matching "
"dependent on the current locale instead of the Unicode database."
msgstr ""
msgid ""
"Locales are a feature of the C library intended to help in writing programs "
"that take account of language differences. For example, if you're "
"processing encoded French text, you'd want to be able to write ``\\w+`` to "
"match words, but ``\\w`` only matches the character class ``[A-Za-z]`` in "
"bytes patterns; it won't match bytes corresponding to ```` or ````. If "
"your system is configured properly and a French locale is selected, certain "
"C functions will tell the program that the byte corresponding to ```` "
"should also be considered a letter. Setting the :const:`LOCALE` flag when "
"compiling a regular expression will cause the resulting compiled object to "
"use these C functions for ``\\w``; this is slower, but also enables ``\\w+`` "
"to match French words as you'd expect. The use of this flag is discouraged "
"in Python 3 as the locale mechanism is very unreliable, it only handles one "
"\"culture\" at a time, and it only works with 8-bit locales. Unicode "
"matching is already enabled by default in Python 3 for Unicode (str) "
"patterns, and it is able to handle different locales/languages."
msgstr ""
msgid ""
"(``^`` and ``$`` haven't been explained yet; they'll be introduced in "
"section :ref:`more-metacharacters`.)"
msgstr ""
msgid ""
"Usually ``^`` matches only at the beginning of the string, and ``$`` matches "
"only at the end of the string and immediately before the newline (if any) at "
"the end of the string. When this flag is specified, ``^`` matches at the "
"beginning of the string and at the beginning of each line within the string, "
"immediately following each newline. Similarly, the ``$`` metacharacter "
"matches either at the end of the string and at the end of each line "
"(immediately preceding each newline)."
msgstr ""
msgid ""
"Makes the ``'.'`` special character match any character at all, including a "
"newline; without this flag, ``'.'`` will match anything *except* a newline."
msgstr ""
msgid ""
"Make ``\\w``, ``\\W``, ``\\b``, ``\\B``, ``\\s`` and ``\\S`` perform ASCII-"
"only matching instead of full Unicode matching. This is only meaningful for "
"Unicode patterns, and is ignored for byte patterns."
msgstr ""
msgid ""
"This flag allows you to write regular expressions that are more readable by "
"granting you more flexibility in how you can format them. When this flag "
"has been specified, whitespace within the RE string is ignored, except when "
"the whitespace is in a character class or preceded by an unescaped "
"backslash; this lets you organize and indent the RE more clearly. This flag "
"also lets you put comments within a RE that will be ignored by the engine; "
"comments are marked by a ``'#'`` that's neither in a character class nor "
"preceded by an unescaped backslash."
msgstr ""
msgid ""
"For example, here's a RE that uses :const:`re.VERBOSE`; see how much easier "
"it is to read? ::"
msgstr ""
msgid ""
"charref = re.compile(r\"\"\"\n"
" &[#] # Start of a numeric entity reference\n"
" (\n"
" 0[0-7]+ # Octal form\n"
" | [0-9]+ # Decimal form\n"
" | x[0-9a-fA-F]+ # Hexadecimal form\n"
" )\n"
" ; # Trailing semicolon\n"
"\"\"\", re.VERBOSE)"
msgstr ""
msgid "Without the verbose setting, the RE would look like this::"
msgstr ""
msgid ""
"charref = re.compile(\"(0[0-7]+\"\n"
" \"|[0-9]+\"\n"
" \"|x[0-9a-fA-F]+);\")"
msgstr ""
"charref = re.compile(\"(0[0-7]+\"\n"
" \"|[0-9]+\"\n"
" \"|x[0-9a-fA-F]+);\")"
msgid ""
"In the above example, Python's automatic concatenation of string literals "
"has been used to break up the RE into smaller pieces, but it's still more "
"difficult to understand than the version using :const:`re.VERBOSE`."
msgstr ""
msgid "More pattern power"
msgstr ""
msgid ""
"So far we've only covered a part of the features of regular expressions. In "
"this section, we'll cover some new metacharacters, and how to use groups to "
"retrieve portions of the text that was matched."
msgstr ""
msgid "More metacharacters"
msgstr ""
msgid ""
"There are some metacharacters that we haven't covered yet. Most of them "
"will be covered in this section."
msgstr ""
msgid ""
"Some of the remaining metacharacters to be discussed are :dfn:`zero-width "
"assertions`. They don't cause the engine to advance through the string; "
"instead, they consume no characters at all, and simply succeed or fail. For "
"example, ``\\b`` is an assertion that the current position is located at a "
"word boundary; the position isn't changed by the ``\\b`` at all. This means "
"that zero-width assertions should never be repeated, because if they match "
"once at a given location, they can obviously be matched an infinite number "
"of times."
msgstr ""
msgid "``|``"
msgstr "``|``"
msgid ""
"Alternation, or the \"or\" operator. If *A* and *B* are regular "
"expressions, ``A|B`` will match any string that matches either *A* or *B*. "
"``|`` has very low precedence in order to make it work reasonably when "
"you're alternating multi-character strings. ``Crow|Servo`` will match either "
"``'Crow'`` or ``'Servo'``, not ``'Cro'``, a ``'w'`` or an ``'S'``, and "
"``'ervo'``."
msgstr ""
msgid ""
"To match a literal ``'|'``, use ``\\|``, or enclose it inside a character "
"class, as in ``[|]``."
msgstr ""
msgid "``^``"
msgstr "``^``"
msgid ""
"Matches at the beginning of lines. Unless the :const:`MULTILINE` flag has "
"been set, this will only match at the beginning of the string. In :const:"
"`MULTILINE` mode, this also matches immediately after each newline within "
"the string."
msgstr ""
msgid ""
"For example, if you wish to match the word ``From`` only at the beginning of "
"a line, the RE to use is ``^From``. ::"
msgstr ""
msgid ""
">>> print(re.search('^From', 'From Here to Eternity'))\n"
"\n"
">>> print(re.search('^From', 'Reciting From Memory'))\n"
"None"
msgstr ""
msgid "To match a literal ``'^'``, use ``\\^``."
msgstr ""
msgid "``$``"
msgstr "``$``"
msgid ""
"Matches at the end of a line, which is defined as either the end of the "
"string, or any location followed by a newline character. ::"
msgstr ""
msgid ""
">>> print(re.search('}$', '{block}'))\n"
"\n"
">>> print(re.search('}$', '{block} '))\n"
"None\n"
">>> print(re.search('}$', '{block}\\n'))\n"
""
msgstr ""
">>> print(re.search('}$', '{block}'))\n"
"\n"
">>> print(re.search('}$', '{block} '))\n"
"None\n"
">>> print(re.search('}$', '{block}\\n'))\n"
""
msgid ""
"To match a literal ``'$'``, use ``\\$`` or enclose it inside a character "
"class, as in ``[$]``."
msgstr ""
msgid "``\\A``"
msgstr "``\\A``"
msgid ""
"Matches only at the start of the string. When not in :const:`MULTILINE` "
"mode, ``\\A`` and ``^`` are effectively the same. In :const:`MULTILINE` "
"mode, they're different: ``\\A`` still matches only at the beginning of the "
"string, but ``^`` may match at any location inside the string that follows a "
"newline character."
msgstr ""
msgid "``\\z``"
msgstr "``\\z``"
msgid "Matches only at the end of the string."
msgstr ""
msgid "``\\Z``"
msgstr "``\\Z``"
msgid "The same as ``\\z``. For compatibility with old Python versions."
msgstr ""
msgid "``\\b``"
msgstr "``\\b``"
msgid ""
"Word boundary. This is a zero-width assertion that matches only at the "
"beginning or end of a word. A word is defined as a sequence of alphanumeric "
"characters, so the end of a word is indicated by whitespace or a non-"
"alphanumeric character."
msgstr ""
msgid ""
"The following example matches ``class`` only when it's a complete word; it "
"won't match when it's contained inside another word. ::"
msgstr ""
msgid ""
">>> p = re.compile(r'\\bclass\\b')\n"
">>> print(p.search('no class at all'))\n"
"\n"
">>> print(p.search('the declassified algorithm'))\n"
"None\n"
">>> print(p.search('one subclass is'))\n"
"None"
msgstr ""
msgid ""
"There are two subtleties you should remember when using this special "
"sequence. First, this is the worst collision between Python's string "
"literals and regular expression sequences. In Python's string literals, "
"``\\b`` is the backspace character, ASCII value 8. If you're not using raw "
"strings, then Python will convert the ``\\b`` to a backspace, and your RE "
"won't match as you expect it to. The following example looks the same as our "
"previous RE, but omits the ``'r'`` in front of the RE string. ::"
msgstr ""
msgid ""
">>> p = re.compile('\\bclass\\b')\n"
">>> print(p.search('no class at all'))\n"
"None\n"
">>> print(p.search('\\b' + 'class' + '\\b'))\n"
""
msgstr ""
">>> p = re.compile('\\bclass\\b')\n"
">>> print(p.search('no class at all'))\n"
"None\n"
">>> print(p.search('\\b' + 'class' + '\\b'))\n"
""
msgid ""
"Second, inside a character class, where there's no use for this assertion, "
"``\\b`` represents the backspace character, for compatibility with Python's "
"string literals."
msgstr ""
msgid "``\\B``"
msgstr "``\\B``"
msgid ""
"Another zero-width assertion, this is the opposite of ``\\b``, only matching "
"when the current position is not at a word boundary."
msgstr ""
msgid "Grouping"
msgstr ""
msgid ""
"Frequently you need to obtain more information than just whether the RE "
"matched or not. Regular expressions are often used to dissect strings by "
"writing a RE divided into several subgroups which match different components "
"of interest. For example, an RFC-822 header line is divided into a header "
"name and a value, separated by a ``':'``, like this:"
msgstr ""
msgid ""
"From: author@example.com\n"
"User-Agent: Thunderbird 1.5.0.9 (X11/20061227)\n"
"MIME-Version: 1.0\n"
"To: editor@example.com"
msgstr ""
"From: author@example.com\n"
"User-Agent: Thunderbird 1.5.0.9 (X11/20061227)\n"
"MIME-Version: 1.0\n"
"To: editor@example.com"
msgid ""
"This can be handled by writing a regular expression which matches an entire "
"header line, and has one group which matches the header name, and another "
"group which matches the header's value."
msgstr ""
msgid ""
"Groups are marked by the ``'('``, ``')'`` metacharacters. ``'('`` and "
"``')'`` have much the same meaning as they do in mathematical expressions; "
"they group together the expressions contained inside them, and you can "
"repeat the contents of a group with a quantifier, such as ``*``, ``+``, ``?"
"``, or ``{m,n}``. For example, ``(ab)*`` will match zero or more "
"repetitions of ``ab``. ::"
msgstr ""
msgid ""
">>> p = re.compile('(ab)*')\n"
">>> print(p.search('ababababab').span())\n"
"(0, 10)"
msgstr ""
msgid ""
"Groups indicated with ``'('``, ``')'`` also capture the starting and ending "
"index of the text that they match; this can be retrieved by passing an "
"argument to :meth:`~re.Match.group`, :meth:`~re.Match.start`, :meth:`~re."
"Match.end`, and :meth:`~re.Match.span`. Groups are numbered starting with "
"0. Group 0 is always present; it's the whole RE, so :ref:`match object "
"` methods all have group 0 as their default argument. Later "
"we'll see how to express groups that don't capture the span of text that "
"they match. ::"
msgstr ""
msgid ""
">>> p = re.compile('(a)b')\n"
">>> m = p.search('ab')\n"
">>> m.group()\n"
"'ab'\n"
">>> m.group(0)\n"
"'ab'"
msgstr ""
msgid ""
"Subgroups are numbered from left to right, from 1 upward. Groups can be "
"nested; to determine the number, just count the opening parenthesis "
"characters, going from left to right. ::"
msgstr ""
msgid ""
">>> p = re.compile('(a(b)c)d')\n"
">>> m = p.search('abcd')\n"
">>> m.group(0)\n"
"'abcd'\n"
">>> m.group(1)\n"
"'abc'\n"
">>> m.group(2)\n"
"'b'"
msgstr ""
msgid ""
":meth:`~re.Match.group` can be passed multiple group numbers at a time, in "
"which case it will return a tuple containing the corresponding values for "
"those groups. ::"
msgstr ""
msgid ""
">>> m.group(2,1,2)\n"
"('b', 'abc', 'b')"
msgstr ""
">>> m.group(2,1,2)\n"
"('b', 'abc', 'b')"
msgid ""
"The :meth:`~re.Match.groups` method returns a tuple containing the strings "
"for all the subgroups, from 1 up to however many there are. ::"
msgstr ""
msgid ""
">>> m.groups()\n"
"('abc', 'b')"
msgstr ""
">>> m.groups()\n"
"('abc', 'b')"
msgid ""
"Backreferences in a pattern allow you to specify that the contents of an "
"earlier capturing group must also be found at the current location in the "
"string. For example, ``\\1`` will succeed if the exact contents of group 1 "
"can be found at the current position, and fails otherwise. Remember that "
"Python's string literals also use a backslash followed by numbers to allow "
"including arbitrary characters in a string, so be sure to use a raw string "
"when incorporating backreferences in a RE."
msgstr ""
msgid "For example, the following RE detects doubled words in a string. ::"
msgstr ""
msgid ""
">>> p = re.compile(r'\\b(\\w+)\\s+\\1\\b')\n"
">>> p.search('Paris in the the spring').group()\n"
"'the the'"
msgstr ""
">>> p = re.compile(r'\\b(\\w+)\\s+\\1\\b')\n"
">>> p.search('Paris in the the spring').group()\n"
"'the the'"
msgid ""
"Backreferences like this aren't often useful for just searching through a "
"string --- there are few text formats which repeat data in this way --- but "
"you'll soon find out that they're *very* useful when performing string "
"substitutions."
msgstr ""
msgid "Non-capturing and named groups"
msgstr ""
msgid ""
"Elaborate REs may use many groups, both to capture substrings of interest, "
"and to group and structure the RE itself. In complex REs, it becomes "
"difficult to keep track of the group numbers. There are two features which "
"help with this problem. Both of them use a common syntax for regular "
"expression extensions, so we'll look at that first."
msgstr ""
msgid ""
"Perl 5 is well known for its powerful additions to standard regular "
"expressions. For these new features the Perl developers couldn't choose new "
"single-keystroke metacharacters or new special sequences beginning with "
"``\\`` without making Perl's regular expressions confusingly different from "
"standard REs. If they chose ``&`` as a new metacharacter, for example, old "
"expressions would be assuming that ``&`` was a regular character and "
"wouldn't have escaped it by writing ``\\&`` or ``[&]``."
msgstr ""
msgid ""
"The solution chosen by the Perl developers was to use ``(?...)`` as the "
"extension syntax. ``?`` immediately after a parenthesis was a syntax error "
"because the ``?`` would have nothing to repeat, so this didn't introduce any "
"compatibility problems. The characters immediately after the ``?`` "
"indicate what extension is being used, so ``(?=foo)`` is one thing (a "
"positive lookahead assertion) and ``(?:foo)`` is something else (a non-"
"capturing group containing the subexpression ``foo``)."
msgstr ""
msgid ""
"Python supports several of Perl's extensions and adds an extension syntax to "
"Perl's extension syntax. If the first character after the question mark is "
"a ``P``, you know that it's an extension that's specific to Python."
msgstr ""
msgid ""
"Now that we've looked at the general extension syntax, we can return to the "
"features that simplify working with groups in complex REs."
msgstr ""
msgid ""
"Sometimes you'll want to use a group to denote a part of a regular "
"expression, but aren't interested in retrieving the group's contents. You "
"can make this fact explicit by using a non-capturing group: ``(?:...)``, "
"where you can replace the ``...`` with any other regular expression. ::"
msgstr ""
msgid ""
">>> m = re.search(\"([abc])+\", \"abc\")\n"
">>> m.groups()\n"
"('c',)\n"
">>> m = re.search(\"(?:[abc])+\", \"abc\")\n"
">>> m.groups()\n"
"()"
msgstr ""
msgid ""
"Except for the fact that you can't retrieve the contents of what the group "
"matched, a non-capturing group behaves exactly the same as a capturing "
"group; you can put anything inside it, repeat it with a repetition "
"metacharacter such as ``*``, and nest it within other groups (capturing or "
"non-capturing). ``(?:...)`` is particularly useful when modifying an "
"existing pattern, since you can add new groups without changing how all the "
"other groups are numbered. It should be mentioned that there's no "
"performance difference in searching between capturing and non-capturing "
"groups; neither form is any faster than the other."
msgstr ""
msgid ""
"A more significant feature is named groups: instead of referring to them by "
"numbers, groups can be referenced by a name."
msgstr ""
msgid ""
"The syntax for a named group is one of the Python-specific extensions: ``(?"
"P...)``. *name* is, obviously, the name of the group. Named groups "
"behave exactly like capturing groups, and additionally associate a name with "
"a group. The :ref:`match object ` methods that deal with "
"capturing groups all accept either integers that refer to the group by "
"number or strings that contain the desired group's name. Named groups are "
"still given numbers, so you can retrieve information about a group in two "
"ways::"
msgstr ""
msgid ""
">>> p = re.compile(r'(?P\\b\\w+\\b)')\n"
">>> m = p.search( '(((( Lots of punctuation )))' )\n"
">>> m.group('word')\n"
"'Lots'\n"
">>> m.group(1)\n"
"'Lots'"
msgstr ""
msgid ""
"Additionally, you can retrieve named groups as a dictionary with :meth:`~re."
"Match.groupdict`::"
msgstr ""
msgid ""
">>> m = re.search(r'(?P\\w+) (?P\\w+)', 'Jane Doe')\n"
">>> m.groupdict()\n"
"{'first': 'Jane', 'last': 'Doe'}"
msgstr ""
msgid ""
"Named groups are handy because they let you use easily remembered names, "
"instead of having to remember numbers. Here's an example RE from the :mod:"
"`imaplib` module::"
msgstr ""
msgid ""
"InternalDate = re.compile(r'INTERNALDATE \"'\n"
" r'(?P[ 123][0-9])-(?P[A-Z][a-z][a-z])-'\n"
" r'(?P[0-9][0-9][0-9][0-9])'\n"
" r' (?P[0-9][0-9]):(?P[0-9][0-9]):(?P[0-9][0-9])'\n"
" r' (?P[-+])(?P[0-9][0-9])(?P[0-9][0-9])'\n"
" r'\"')"
msgstr ""
"InternalDate = re.compile(r'INTERNALDATE \"'\n"
" r'(?P[ 123][0-9])-(?P[A-Z][a-z][a-z])-'\n"
" r'(?P[0-9][0-9][0-9][0-9])'\n"
" r' (?P[0-9][0-9]):(?P[0-9][0-9]):(?P[0-9][0-9])'\n"
" r' (?P[-+])(?P[0-9][0-9])(?P[0-9][0-9])'\n"
" r'\"')"
msgid ""
"It's obviously much easier to retrieve ``m.group('zonem')``, instead of "
"having to remember to retrieve group 9."
msgstr ""
msgid ""
"The syntax for backreferences in an expression such as ``(...)\\1`` refers "
"to the number of the group. There's naturally a variant that uses the group "
"name instead of the number. This is another Python extension: ``(?P=name)`` "
"indicates that the contents of the group called *name* should again be "
"matched at the current point. The regular expression for finding doubled "
"words, ``\\b(\\w+)\\s+\\1\\b`` can also be written as ``\\b(?"
"P\\w+)\\s+(?P=word)\\b``::"
msgstr ""
msgid ""
">>> p = re.compile(r'\\b(?P\\w+)\\s+(?P=word)\\b')\n"
">>> p.search('Paris in the the spring').group()\n"
"'the the'"
msgstr ""
">>> p = re.compile(r'\\b(?P\\w+)\\s+(?P=word)\\b')\n"
">>> p.search('Paris in the the spring').group()\n"
"'the the'"
msgid "Lookahead assertions"
msgstr ""
msgid ""
"Another zero-width assertion is the lookahead assertion. Lookahead "
"assertions are available in both positive and negative form, and look like "
"this:"
msgstr ""
msgid "``(?=...)``"
msgstr "``(?=...)``"
msgid ""
"Positive lookahead assertion. This succeeds if the contained regular "
"expression, represented here by ``...``, successfully matches at the current "
"location, and fails otherwise. But, once the contained expression has been "
"tried, the matching engine doesn't advance at all; the rest of the pattern "
"is tried right where the assertion started."
msgstr ""
msgid "``(?!...)``"
msgstr "``(?!...)``"
msgid ""
"Negative lookahead assertion. This is the opposite of the positive "
"assertion; it succeeds if the contained expression *doesn't* match at the "
"current position in the string."
msgstr ""
msgid ""
"To make this concrete, let's look at a case where a lookahead is useful. "
"Consider a simple pattern to match a filename and split it apart into a base "
"name and an extension, separated by a ``.``. For example, in ``news.rc``, "
"``news`` is the base name, and ``rc`` is the filename's extension."
msgstr ""
msgid "The pattern to match this is quite simple:"
msgstr ""
msgid "``.*[.].*$``"
msgstr "``.*[.].*$``"
msgid ""
"Notice that the ``.`` needs to be treated specially because it's a "
"metacharacter, so it's inside a character class to only match that specific "
"character. Also notice the trailing ``$``; this is added to ensure that all "
"the rest of the string must be included in the extension. This regular "
"expression matches ``foo.bar`` and ``autoexec.bat`` and ``sendmail.cf`` and "
"``printers.conf``."
msgstr ""
msgid ""
"Now, consider complicating the problem a bit; what if you want to match "
"filenames where the extension is not ``bat``? Some incorrect attempts:"
msgstr ""
msgid "``.*[.][^b].*$``"
msgstr "``.*[.][^b].*$``"
msgid ""
"The first attempt above tries to exclude ``bat`` by requiring that the first "
"character of the extension is not a ``b``. This is wrong, because the "
"pattern also doesn't match ``foo.bar``."
msgstr ""
msgid "``.*[.]([^b]..|.[^a].|..[^t])$``"
msgstr "``.*[.]([^b]..|.[^a].|..[^t])$``"
msgid ""
"The expression gets messier when you try to patch up the first solution by "
"requiring one of the following cases to match: the first character of the "
"extension isn't ``b``; the second character isn't ``a``; or the third "
"character isn't ``t``. This accepts ``foo.bar`` and rejects ``autoexec."
"bat``, but it requires a three-letter extension and won't accept a filename "
"with a two-letter extension such as ``sendmail.cf``. We'll complicate the "
"pattern again in an effort to fix it."
msgstr ""
msgid "``.*[.]([^b].?.?|.[^a]?.?|..?[^t]?)$``"
msgstr "``.*[.]([^b].?.?|.[^a]?.?|..?[^t]?)$``"
msgid ""
"In the third attempt, the second and third letters are all made optional in "
"order to allow matching extensions shorter than three characters, such as "
"``sendmail.cf``."
msgstr ""
msgid ""
"The pattern's getting really complicated now, which makes it hard to read "
"and understand. Worse, if the problem changes and you want to exclude both "
"``bat`` and ``exe`` as extensions, the pattern would get even more "
"complicated and confusing."
msgstr ""
msgid "A negative lookahead cuts through all this confusion:"
msgstr ""
msgid "``.*[.](?!bat$)[^.]*$``"
msgstr "``.*[.](?!bat$)[^.]*$``"
msgid ""
"The negative lookahead means: if the expression ``bat`` doesn't match at "
"this point, try the rest of the pattern; if ``bat$`` does match, the whole "
"pattern will fail. The trailing ``$`` is required to ensure that something "
"like ``sample.batch``, where the extension only starts with ``bat``, will be "
"allowed. The ``[^.]*`` makes sure that the pattern works when there are "
"multiple dots in the filename."
msgstr ""
msgid ""
"Excluding another filename extension is now easy; simply add it as an "
"alternative inside the assertion. The following pattern excludes filenames "
"that end in either ``bat`` or ``exe``:"
msgstr ""
msgid "``.*[.](?!bat$|exe$)[^.]*$``"
msgstr "``.*[.](?!bat$|exe$)[^.]*$``"
msgid "Modifying strings"
msgstr ""
msgid ""
"Up to this point, we've simply performed searches against a static string. "
"Regular expressions are also commonly used to modify strings in various "
"ways, using the following pattern methods:"
msgstr ""
msgid "``split()``"
msgstr "``split()``"
msgid "Split the string into a list, splitting it wherever the RE matches"
msgstr ""
msgid "``sub()``"
msgstr "``sub()``"
msgid ""
"Find all substrings where the RE matches, and replace them with a different "
"string"
msgstr ""
msgid "``subn()``"
msgstr "``subn()``"
msgid ""
"Does the same thing as :meth:`!sub`, but returns the new string and the "
"number of replacements"
msgstr ""
msgid "Splitting strings"
msgstr ""
msgid ""
"The :meth:`~re.Pattern.split` method of a pattern splits a string apart "
"wherever the RE matches, returning a list of the pieces. It's similar to "
"the :meth:`~str.split` method of strings but provides much more generality "
"in the delimiters that you can split by; string :meth:`!split` only supports "
"splitting by whitespace or by a fixed string. As you'd expect, there's a "
"module-level :func:`re.split` function, too."
msgstr ""
msgid ""
"Split *string* by the matches of the regular expression. If capturing "
"parentheses are used in the RE, then their contents will also be returned as "
"part of the resulting list. If *maxsplit* is nonzero, at most *maxsplit* "
"splits are performed."
msgstr ""
msgid ""
"You can limit the number of splits made, by passing a value for *maxsplit*. "
"When *maxsplit* is nonzero, at most *maxsplit* splits will be made, and the "
"remainder of the string is returned as the final element of the list. In "
"the following example, the delimiter is any sequence of non-alphanumeric "
"characters. ::"
msgstr ""
msgid ""
">>> p = re.compile(r'\\W+')\n"
">>> p.split('This is a test, short and sweet, of split().')\n"
"['This', 'is', 'a', 'test', 'short', 'and', 'sweet', 'of', 'split', '']\n"
">>> p.split('This is a test, short and sweet, of split().', 3)\n"
"['This', 'is', 'a', 'test, short and sweet, of split().']"
msgstr ""
msgid ""
"Sometimes you're not only interested in what the text between delimiters is, "
"but also need to know what the delimiter was. If capturing parentheses are "
"used in the RE, then their values are also returned as part of the list. "
"Compare the following calls::"
msgstr ""
msgid ""
">>> p = re.compile(r'\\W+')\n"
">>> p2 = re.compile(r'(\\W+)')\n"
">>> p.split('This... is a test.')\n"
"['This', 'is', 'a', 'test', '']\n"
">>> p2.split('This... is a test.')\n"
"['This', '... ', 'is', ' ', 'a', ' ', 'test', '.', '']"
msgstr ""
msgid ""
"The module-level function :func:`re.split` adds the RE to be used as the "
"first argument, but is otherwise the same. ::"
msgstr ""
msgid ""
">>> re.split(r'[\\W]+', 'Words, words, words.')\n"
"['Words', 'words', 'words', '']\n"
">>> re.split(r'([\\W]+)', 'Words, words, words.')\n"
"['Words', ', ', 'words', ', ', 'words', '.', '']\n"
">>> re.split(r'[\\W]+', 'Words, words, words.', 1)\n"
"['Words', 'words, words.']"
msgstr ""
msgid "Search and replace"
msgstr ""
msgid ""
"Another common task is to find all the matches for a pattern, and replace "
"them with a different string. The :meth:`~re.Pattern.sub` method takes a "
"replacement value, which can be either a string or a function, and the "
"string to be processed."
msgstr ""
msgid ""
"Returns the string obtained by replacing the leftmost non-overlapping "
"occurrences of the RE in *string* by the replacement *replacement*. If the "
"pattern isn't found, *string* is returned unchanged."
msgstr ""
msgid ""
"The optional argument *count* is the maximum number of pattern occurrences "
"to be replaced; *count* must be a non-negative integer. The default value "
"of 0 means to replace all occurrences."
msgstr ""
msgid ""
"Here's a simple example of using the :meth:`~re.Pattern.sub` method. It "
"replaces colour names with the word ``colour``::"
msgstr ""
msgid ""
">>> p = re.compile('(blue|white|red)')\n"
">>> p.sub('colour', 'blue socks and red shoes')\n"
"'colour socks and colour shoes'\n"
">>> p.sub('colour', 'blue socks and red shoes', count=1)\n"
"'colour socks and red shoes'"
msgstr ""
msgid ""
"The :meth:`~re.Pattern.subn` method does the same work, but returns a 2-"
"tuple containing the new string value and the number of replacements that "
"were performed::"
msgstr ""
msgid ""
">>> p = re.compile('(blue|white|red)')\n"
">>> p.subn('colour', 'blue socks and red shoes')\n"
"('colour socks and colour shoes', 2)\n"
">>> p.subn('colour', 'no colours at all')\n"
"('no colours at all', 0)"
msgstr ""
msgid ""
"Empty matches are replaced only when they're not adjacent to a previous "
"empty match. ::"
msgstr ""
msgid ""
">>> p = re.compile('x*')\n"
">>> p.sub('-', 'abxd')\n"
"'-a-b--d-'"
msgstr ""
">>> p = re.compile('x*')\n"
">>> p.sub('-', 'abxd')\n"
"'-a-b--d-'"
msgid ""
"If *replacement* is a string, any backslash escapes in it are processed. "
"That is, ``\\n`` is converted to a single newline character, ``\\r`` is "
"converted to a carriage return, and so forth. Unknown escapes such as "
"``\\&`` are left alone. Backreferences, such as ``\\6``, are replaced with "
"the substring matched by the corresponding group in the RE. This lets you "
"incorporate portions of the original text in the resulting replacement "
"string."
msgstr ""
msgid ""
"This example matches the word ``section`` followed by a string enclosed in "
"``{``, ``}``, and changes ``section`` to ``subsection``::"
msgstr ""
msgid ""
">>> p = re.compile('section{ ( [^}]* ) }', re.VERBOSE)\n"
">>> p.sub(r'subsection{\\1}','section{First} section{second}')\n"
"'subsection{First} subsection{second}'"
msgstr ""
">>> p = re.compile('section{ ( [^}]* ) }', re.VERBOSE)\n"
">>> p.sub(r'subsection{\\1}','section{First} section{second}')\n"
"'subsection{First} subsection{second}'"
msgid ""
"There's also a syntax for referring to named groups as defined by the ``(?"
"P...)`` syntax. ``\\g`` will use the substring matched by the "
"group named ``name``, and ``\\g`` uses the corresponding group "
"number. ``\\g`` is therefore equivalent to ``\\2``, but isn't ambiguous "
"in a replacement string such as ``\\g0``. (``\\20`` would be interpreted "
"as a reference to group 20, not a reference to group 2 followed by the "
"literal character ``'0'``.) The following substitutions are all equivalent, "
"but use all three variations of the replacement string. ::"
msgstr ""
msgid ""
">>> p = re.compile('section{ (?P [^}]* ) }', re.VERBOSE)\n"
">>> p.sub(r'subsection{\\1}','section{First}')\n"
"'subsection{First}'\n"
">>> p.sub(r'subsection{\\g}','section{First}')\n"
"'subsection{First}'\n"
">>> p.sub(r'subsection{\\g}','section{First}')\n"
"'subsection{First}'"
msgstr ""
msgid ""
"*replacement* can also be a function, which gives you even more control. If "
"*replacement* is a function, the function is called for every non-"
"overlapping occurrence of *pattern*. On each call, the function is passed "
"a :ref:`match object ` argument for the match and can use "
"this information to compute the desired replacement string and return it."
msgstr ""
msgid ""
"In the following example, the replacement function translates decimals into "
"hexadecimal::"
msgstr ""
msgid ""
">>> def hexrepl(match):\n"
"... \"Return the hex string for a decimal number\"\n"
"... value = int(match.group())\n"
"... return hex(value)\n"
"...\n"
">>> p = re.compile(r'\\d+')\n"
">>> p.sub(hexrepl, 'Call 65490 for printing, 49152 for user code.')\n"
"'Call 0xffd2 for printing, 0xc000 for user code.'"
msgstr ""
msgid ""
"When using the module-level :func:`re.sub` function, the pattern is passed "
"as the first argument. The pattern may be provided as an object or as a "
"string; if you need to specify regular expression flags, you must either use "
"a pattern object as the first parameter, or use embedded modifiers in the "
"pattern string, e.g. ``sub(\"(?i)b+\", \"x\", \"bbbb BBBB\")`` returns ``'x "
"x'``."
msgstr ""
msgid "Common problems"
msgstr ""
msgid ""
"Regular expressions are a powerful tool for some applications, but in some "
"ways their behaviour isn't intuitive and at times they don't behave the way "
"you may expect them to. This section will point out some of the most common "
"pitfalls."
msgstr ""
msgid "Use string methods"
msgstr ""
msgid ""
"Sometimes using the :mod:`re` module is a mistake. If you're matching a "
"fixed string, or a single character class, and you're not using any :mod:"
"`re` features such as the :const:`~re.IGNORECASE` flag, then the full power "
"of regular expressions may not be required. Strings have several methods for "
"performing operations with fixed strings and they're usually much faster, "
"because the implementation is a single small C loop that's been optimized "
"for the purpose, instead of the large, more generalized regular expression "
"engine."
msgstr ""
msgid ""
"One example might be replacing a single fixed string with another one; for "
"example, you might replace ``word`` with ``deed``. :func:`re.sub` seems "
"like the function to use for this, but consider the :meth:`~str.replace` "
"method. Note that :meth:`!replace` will also replace ``word`` inside words, "
"turning ``swordfish`` into ``sdeedfish``, but the naive RE ``word`` would "
"have done that, too. (To avoid performing the substitution on parts of "
"words, the pattern would have to be ``\\bword\\b``, in order to require that "
"``word`` have a word boundary on either side. This takes the job beyond :"
"meth:`!replace`'s abilities.)"
msgstr ""
msgid ""
"Another common task is deleting every occurrence of a single character from "
"a string or replacing it with another single character. You might do this "
"with something like ``re.sub('\\n', ' ', S)``, but :meth:`~str.translate` is "
"capable of doing both tasks and will be faster than any regular expression "
"operation can be."
msgstr ""
msgid ""
"In short, before turning to the :mod:`re` module, consider whether your "
"problem can be solved with a faster and simpler string method."
msgstr ""
msgid "prefixmatch() (aka match) versus search()"
msgstr ""
msgid ""
":func:`~re.prefixmatch` was added in Python 3.15 as the :ref:`preferred name "
"` for :func:`~re.match`. Before this, it was only "
"known as :func:`!match` and the distinction with :func:`~re.search` was "
"often misunderstood."
msgstr ""
msgid ""
":func:`!prefixmatch` aka :func:`!match` only checks if the RE matches at the "
"beginning of the string while :func:`!search` scans forward through the "
"string for a match. ::"
msgstr ""
msgid ""
">>> print(re.prefixmatch('super', 'superstition').span())\n"
"(0, 5)\n"
">>> print(re.prefixmatch('super', 'insuperable'))\n"
"None"
msgstr ""
msgid ""
"On the other hand, :func:`~re.search` scans forward through the string, "
"reporting the first match it finds. ::"
msgstr ""
msgid ""
">>> print(re.search('super', 'superstition').span())\n"
"(0, 5)\n"
">>> print(re.search('super', 'insuperable').span())\n"
"(2, 7)"
msgstr ""
">>> print(re.search('super', 'superstition').span())\n"
"(0, 5)\n"
">>> print(re.search('super', 'insuperable').span())\n"
"(2, 7)"
msgid ""
"This distinction is important to remember when using the old :func:`~re."
"match` name in code requiring compatibility with older Python versions."
msgstr ""
msgid "Greedy versus non-greedy"
msgstr ""
msgid ""
"When repeating a regular expression, as in ``a*``, the resulting action is "
"to consume as much of the pattern as possible. This fact often bites you "
"when you're trying to match a pair of balanced delimiters, such as the angle "
"brackets surrounding an HTML tag. The naive pattern for matching a single "
"HTML tag doesn't work because of the greedy nature of ``.*``. ::"
msgstr ""
msgid ""
">>> s = 'Title'\n"
">>> len(s)\n"
"32\n"
">>> print(re.prefixmatch('', s).span())\n"
"(0, 32)\n"
">>> print(re.prefixmatch('', s).group())\n"
"Title"
msgstr ""
msgid ""
"The RE matches the ``'`` "
"can't match at the end of the string, so the regular expression engine has "
"to backtrack character by character until it finds a match for the ``>``. "
"The final match extends from the ``''`` in "
"``''``, which isn't what you want."
msgstr ""
msgid ""
"In this case, the solution is to use the non-greedy quantifiers ``*?``, ``+?"
"``, ``??``, or ``{m,n}?``, which match as *little* text as possible. In the "
"above example, the ``'>'`` is tried immediately after the first ``''`` at every step. This produces just the right result::"
msgstr ""
msgid ""
">>> print(re.prefixmatch('', s).group())\n"
""
msgstr ""
msgid ""
"(Note that parsing HTML or XML with regular expressions is painful. Quick-"
"and-dirty patterns will handle common cases, but HTML and XML have special "
"cases that will break the obvious regular expression; by the time you've "
"written a regular expression that handles all of the possible cases, the "
"patterns will be *very* complicated. Use an HTML or XML parser module for "
"such tasks.)"
msgstr ""
msgid "Using re.VERBOSE"
msgstr "Uyj komendy re.VERBOSE"
msgid ""
"By now you've probably noticed that regular expressions are a very compact "
"notation, but they're not terribly readable. REs of moderate complexity can "
"become lengthy collections of backslashes, parentheses, and metacharacters, "
"making them difficult to read and understand."
msgstr ""
msgid ""
"For such REs, specifying the :const:`re.VERBOSE` flag when compiling the "
"regular expression can be helpful, because it allows you to format the "
"regular expression more clearly."
msgstr ""
msgid ""
"The ``re.VERBOSE`` flag has several effects. Whitespace in the regular "
"expression that *isn't* inside a character class is ignored. This means "
"that an expression such as ``dog | cat`` is equivalent to the less readable "
"``dog|cat``, but ``[a b]`` will still match the characters ``'a'``, ``'b'``, "
"or a space. In addition, you can also put comments inside a RE; comments "
"extend from a ``#`` character to the next newline. When used with triple-"
"quoted strings, this enables REs to be formatted more neatly::"
msgstr ""
msgid ""
"pat = re.compile(r\"\"\"\n"
" \\s* # Skip leading whitespace\n"
" (?P[^:]+) # Header name\n"
" \\s* : # Whitespace, and a colon\n"
" (?P.*?) # The header's value -- *? used to\n"
" # lose the following trailing whitespace\n"
" \\s*$ # Trailing whitespace to end-of-line\n"
"\"\"\", re.VERBOSE)"
msgstr ""
msgid "This is far more readable than::"
msgstr "Jest to o wiele bardziej czytelne ni::"
msgid "pat = re.compile(r\"\\s*(?P[^:]+)\\s*:(?P.*?)\\s*$\")"
msgstr "pat = re.compile(r\"\\s*(?P[^:]+)\\s*:(?P.*?)\\s*$\")"
msgid "Feedback"
msgstr "Feedback"
msgid ""
"Regular expressions are a complicated topic. Did this document help you "
"understand them? Were there parts that were unclear, or problems you "
"encountered that weren't covered here? If so, please send suggestions for "
"improvements to the :ref:`issue tracker `."
msgstr ""
msgid ""
"The most complete book on regular expressions is almost certainly Jeffrey "
"Friedl's Mastering Regular Expressions, published by O'Reilly. "
"Unfortunately, it exclusively concentrates on Perl and Java's flavours of "
"regular expressions, and doesn't contain any Python material at all, so it "
"won't be useful as a reference for programming in Python. (The first "
"edition covered Python's now-removed :mod:`!regex` module, which won't help "
"you much.) Consider checking it out from your library."
msgstr ""