正則表達式

(由Regex跳轉過嚟)

正則表達式英文regular expression,簡稱 regexpregex 或者 RE;台譯正規表式),係指一種描述想搵乜嘢特定嘅字串嘅一種公式,用呢種公式可以喺一段字符當中搵出某串特定嘅字符,好似係[1]

發明正規表式嘅數學家 Stephen Cole Kleene
^The(搵出以 The 做開頭嘅字符串)
end$(搵出以 end 做結尾嘅字符串)
^The end$(搵出同「The end 」完美符合嘅字符串)

... 呀噉。

狹義上,正則表達式指喺1951年由數學家 Stephen Cole Kleene 發明嘅版本[2],喺呢個版本嘅正則表達式,淨係有串連、分組(括弧)、交替(∪,而今一般寫 |)同重複(*,叫做 Kleene star)。意思同而今一般嘅理解有啲唔同,亦都唔係用來搵字串。狹義嘅正則表達式嘅數學基礎係,而佢描述嘅其實係有限狀態機,而且對應嘅係決定性有限狀態機[3]:69–70

廣義上,喺現實當中(例如程式編寫)實際用嘅正則表達式好多時都加入咗好多非決定性嘅元素同其他功能,狹義上並唔 「正則」。現實中經常用到嘅正則表達式包括寫咗入 ISO/IEC 9945-2(POSIX)標準嘅嗰幾種、Perl 自己嘅嗰套同衍生出來嘅 「Perl 相容正則表達式」(PCRE,包括 InDesign 嘅 「GREP 樣式」 功能)、同 Javascript 嘅正則表達式等等。

並唔係所有搵字串嘅公式都係正則表達式。例如 Microsoft Word 嘅所謂 「萬用字元」(wildcard)功能同 Lua 嘅搵字串功能(patterns)都冇 「交替」[4][5],所以唔係正則表達式。

應用 編輯

建立語義網絡 編輯

Regex 可以攞嚟幫手建立語義網絡

淨係上位詞同下位詞嘅關係,已經可以由某啲形式嘅短語透露,例如如果「X 同第啲 Y」噉嘅短語成日出現(regex),噉 Y 就應該係 X 嘅上位詞[6]:p. 122

... 呀噉。

睇埋 編輯

參考資料 編輯

  1. Aho, Alfred V. (1990). "Algorithms for finding patterns in strings.". 出自 van Leeuwen, Jan (編). Algorithms and Complexity. Handbook of Theoretical Computer Science.第A卷. MIT Press. pp. 255–300. doi:10.1016/B978-0-444-88071-0.50010-2.
  2. 參見 Kleene, Stephen Cole. "Representation of events in nerve nets and finite automata" (Research memorandum). Rand Corporation. p. 81.
  3. Lewis, Harry R.; Papadimitriou, Christos H. (1981). Elements of the theory of computation. Englewood Cliffs, NJ: Prentice-Hall.
  4. Mayor, Graham. "Finding and replacing characters using wildcards". The Word MVP Site. 原著喺2021-03-09歸檔. 喺2022年9月28號搵到.
  5. Ierusalimschy, Roberto; de Figueiredo, Luiz Henrique; Celes, Waldemar (2022). "Patterns". Lua 5.4 Reference Manual. 喺2022年9月28號搵到.
  6. Bird, S., Klein, E., & Loper, E. (2009). Natural language processing with Python: analyzing text with the natural language toolkit. O'Reilly Media, Inc.