<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ### 搜索和替換 其他常見的用途就是找到所有模式匹配的字符串并用不同的字符串來替換它們。sub() 方法提供一個替換值,可以是字符串或一個函數,和一個要被處理的字符串。 ``` sub(replacement, string[, count = 0]) ``` 返回的字符串是在字符串中用 RE 最左邊不重復的匹配來替換。如果模式沒有發現,字符將被沒有改變地返回。 可選參數 count 是模式匹配后替換的最大次數;count 必須是非負整數。缺省值是 0 表示替換所有的匹配。 這里有個使用 sub() 方法的簡單例子。它用單詞 "colour" 替換顏色名。 ``` #!python >>> p = re.compile( '(blue|white|red)') >>> p.sub( 'colour', 'blue socks and red shoes') 'colour socks and colour shoes' >>> p.sub( 'colour', 'blue socks and red shoes', count=1) 'colour socks and red shoes' ``` subn() 方法作用一樣,但返回的是包含新字符串和替換執行次數的兩元組。 ``` #!python >>> p = re.compile( '(blue|white|red)') >>> p.subn( 'colour', 'blue socks and red shoes') ('colour socks and colour shoes', 2) >>> p.subn( 'colour', 'no colours at all') ('no colours at all', 0) ``` 空匹配只有在它們沒有緊挨著前一個匹配時才會被替換掉。 ``` #!python >>> p = re.compile('x*') >>> p.sub('-', 'abxd') '-a-b-d-' ``` 如果替換的是一個字符串,任何在其中的反斜杠都會被處理。"\n" 將會被轉換成一個換行符,"\r"轉換成回車等等。未知的轉義如 "\j" 則保持原樣。逆向引用,如 "\6",被 RE 中相應的組匹配而被子串替換。這使你可以在替換后的字符串中插入原始文本的一部分。 這個例子匹配被 "{" 和 "}" 括起來的單詞 "section",并將 "section" 替換成 "subsection"。 ``` #!python >>> p = re.compile('section{ ( [^}]* ) }', re.VERBOSE) >>> p.sub(r'subsection{\1}','section{First} section{second}') 'subsection{First} subsection{second}' ``` 還可以指定用 (?P&lt;name&gt;...) 語法定義的命名組。"\g&lt;name&gt;" 將通過組名 "name" 用子串來匹配,并且 "\g&lt;number&gt;" 使用相應的組號。所以 "\g&lt;2&gt;" 等于 "\2",但能在替換字符串里含義不清,如 "\g&lt;2&gt;0"。("\20" 被解釋成對組 20 的引用,而不是對后面跟著一個字母 "0" 的組 2 的引用。) ``` #!python >>> p = re.compile('section{ (?P<name> [^}]* ) }', re.VERBOSE) >>> p.sub(r'subsection{\1}','section{First}') 'subsection{First}' >>> p.sub(r'subsection{\g<1>}','section{First}') 'subsection{First}' >>> p.sub(r'subsection{\g<name>}','section{First}') 'subsection{First}' ``` 替換也可以是一個甚至給你更多控制的函數。如果替換是個函數,該函數將會被模式中每一個不重復的匹配所調用。在每次調用時,函數會被傳入一個 `MatchObject` 的對象作為參數,因此可以用這個對象去計算出替換字符串并返回它。 在下面的例子里,替換函數將十進制翻譯成十六進制: ``` #!python >>> def hexrepl( match ): ... "Return the hex string for a decimal number" ... value = int( match.group() ) ... return hex(value) ... >>> p = re.compile(r'\d+') >>> p.sub(hexrepl, 'Call 65490 for printing, 49152 for user code.') 'Call 0xffd2 for printing, 0xc000 for user code.' ``` 當使用模塊級的 re.sub() 函數時,模式作為第一個參數。模式也許是一個字符串或一個 `RegexObject`;如果你需要指定正則表達式標志,你必須要么使用 `RegexObject` 做第一個參數,或用使用模式內嵌修正器,如 sub("(?i)b+", "x", "bbbb BBBB") returns 'x x'。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看