跳到主要內容

[Python] 正規表達式 Regex:group

記錄正規表達式的 group 擷取字串的順序。
Python 的正規表達式,透過 group() 函式擷取括號框住的內容。

以下是我寫的例子:
import re
def main():
    string_to_search_1 = "food   @123"
    string_to_search_2 = "feed"
    pattern = re.compile(r'(f(oo|ee)d)\s*(@(\d+))*')

    matches = pattern.finditer(string_to_search_1)
    for match in matches:
        print "match:", match.group()
        print "1:", match.group(1)
        print "2:", match.group(2)
        print "3:", match.group(3)
        print "4:", match.group(4)
        # print "5:", match.group(5)  # no such group.

        tmp = int(match.group(4))
        tmp += 5
        print "tmp =", tmp
        tmp = str(match.group(4))
        print "tmp[2] =", tmp[2]

    matches = pattern.finditer(string_to_search_2)
    for match in matches:
        print "match:", match.group()
        print "1:", match.group(1)
        print "2:", match.group(2)
        print "3:", match.group(3)  #
        print "4:", match.group(4)  #
        # print "5:", match.group(5)  # no such group.

        if match.group(4) is None: print " None detected in group(4)."
        tmp = str(match.group(4))
        print "tmp:", tmp

if(__name__ == "__main__"):
    main()


執行結果
match: food   @123
1: food
2: oo
3: @123
4: 123
tmp = 128
tmp[2] = 3
match: feed
1: feed
2: ee
3: None
4: None
 None detected in group(4).
tmp: None

幾個重點
  1. 在正規表達式內,有幾個括號,就有幾組 group。
  2. 在 string_to_search_1 內掃描 (f(oo|ee)d) 這個 Pattern,group(1) 會回傳 food,group(2) 回傳 "oo"。先從最外大括號先回傳,其次再回傳內部小括號的字串。
  3. 回傳的字串可以自由的轉成整數或是取出個別字元。
  4. 在 string_to_search_2 內,group(3) 跟 group(4) 都回傳 None,因為找不到對應 Pattern。

留言

這個網誌中的熱門文章

[Linux] Elementary OS 字體調校

用 gesetting 取得 elementary os 的等寬字體(這也是終端機默認字體): gsettings get org.gnome.desktop.interface monospace-font-name 會顯示目前字型跟字體大小: Roboto Mono 10 設定字體大小: gsettings set org.gnome.desktop.interface monospace-font-name 'Roboto Mono 12' 可以微調 text-scaling-factor: gsettings set org.gnome.desktop.interface text-scaling-factor <value>

[Vim] 我的 .vimrc

我用過的文字編輯器有 sublime text/atom/notepad++/gedit 等等。 後來我在 window 和 Linux 寫程式還是比較慣用 vim。 以下整理我常用的 .vimrc:  set nu "show line number. set rnu "related line number. nmap ; : "replace ';' with ':' in normal mode. set ai "auto indent set mouse=a "activate mouse set expandtab "replace tab with space. set tabstop=4 "replace tab with 4 spaces. set bg=dark "background set to dark. color desert "color scheme. syntax on "enable syntax highlight. " auto complete: inoremap ( ()<Esc>i inoremap ' ''<Esc>i inoremap " ""<Esc>i inoremap [ []<Esc>i inoremap { {}<Esc>i set cursorline " active cursorline 到此 vim 搭配 Ctrl + N 當自動補全,已經很好用了,之後有好用設定會再補上來。

[C++] 計算程式執行時間:clock 和 time 函式

要計算一段程式碼執行時間,可以用 time 函式: 1 2 3 4 5 time_t start, end; start = time(NULL); // DO SOMETHING... end = time(NULL); cout " [Msg] total time: " difftime(end, start) " sec" endl; time 函式的精確度只到達秒,如果要精確到毫秒等級,要用 clock 函式: 1 2 3 4 5 6 clock_t start, end; start = clock(); // DO SOMETHING... end = clock(); cout ( double )(finish - start) / CLOCKS_PER_SEC endl;