<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 在 if 語句中使用正則表達式 > Some people, when confronted with a problem think; _I know, I’ll use regular expressions._ Now they have two problems. > > — Jamie Zawinski 你可以在 if 語句中使用另一種類型的表達式,即 **正則表達式**(**regular expression**)。 正則表達式是一種使用模式匹配的強大的字符串比較方式。 #### 操作步驟 在你的配置清單中添加如下代碼: ``` if $lsbdistdescription =~ /LTS/ { notice("Looks like you are using a Long Term Support version of Ubuntu.") } else { notice("You might want to upgrade to a Long Term Support version of Ubuntu...") } ``` #### 工作原理 Puppet 將兩個斜線之間的文本當做正則表達式對待,兩個各斜線之間的文本就是要匹配的內容。 如果正則表達式匹配成功,if 表達式為真,第一個大括號之間的代碼就會被執行。 若要使用匹配的反邏輯,即不匹配,需要使用 !~ 替代 =~ ,例如: ``` if $lsbdistdescription !~ /LTS/ { ``` #### 更多用法 正如 Jamie Zawinski 指出的,正則表達式雖然強大,但難于理解和調試。 如果你發現自己所寫的正則表達式相當復雜,以至于一眼看上去不能被理解, 就應該考慮簡化設計使正則表達式更易懂。 然而,正則表達式的一個特別有用的功能是能夠捕獲模式。 ##### 捕捉模式 你不僅可以使用正則表達式匹配文本,還可以捕獲匹配的文本并將其存儲在變量中: ``` $input = "Puppet is better than manual configuration" if $input =~ /(.*) is better than (.*)/ { notice("You said '$0'. Looks like you're comparing $1 to $2!") } ``` 結果為: ``` You said 'Puppet is better than manual configuration'. Looks like you're comparing Puppet to manual configuration! ``` 變量 $0 存儲了所有匹配的文本(假設整體匹配成功)。 如果你將一部分正則表達式置于一對圓括號中,就會創建一個 **組**(**group**), 并且所有被匹配的組都會存儲在變量中。 第一個被匹配的組是 $1,第二個是 $2,以此類推,正如上例展示的那樣。 ##### 正則表達式語法 Puppet 使用了 Ruby 正則表達式語法(Regular expression syntax)的一個子集,如果你還不熟悉正則表達式, 下面網址中的解釋會對你有用: [http://gnosis.cx/publish/programming/regular_expressions.html](http://gnosis.cx/publish/programming/regular_expressions.html) 。 #### 參見本書 * 本章的 [使用正則表達式替換](#ch03sec11) 一節
                  <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>

                              哎呀哎呀视频在线观看