[39] [DFN[[RUBYB[[[正規表現]]]@en[regular expression]]]] ([DFN[[[正則表現]]]]) は、
[[正規言語]]を記述できる[[言語]]です。
ただし、通常用いられるものは[[文脈自由言語]]やそれ以上の[[言語]]を表現できるよう拡張されています。

[51] [[Unix]] の各種ツールや [[Perl]] などの[[プログラミング言語]]、
[[Emacs]] などの[[エディター]]など、非常に多くの[[ソフトウェア]]で採用されています。
ツールや言語によって様々な構文と意味のバリエーションがあり、
「[[正規表現]]」はそうした多数の[[言語]]の総称と言えます。

* 名称・略称

[23]
正規表現 = Regular expression でして、その略称としては
regex と regexp がともによく使われます。

[57] [[正規言語]] ([[正則言語]]) の表記法を指す時は、[[正則表現]]と呼ぶのが普通です。
[[正規表現]]と呼ぶ時のほとんどは、[[正則言語]]以上のものを表現できる拡張された表記法を指しています。

* 標準化

[8] [[正規表現]]には色々なバリエーションがあり、[[正規表現]]一般の[[標準]]は存在しない状態です。
互いに矛盾する機能もあり、完全な[[標準化]]は今後もあり得ないと思われます。

[61] 90年代後半から00年代にかけては、 [[Perl]] が[[正規表現]]の代表的な実装の1つであり、
活発に新機能を追加していました。 [[PCRE]] をはじめ他の色々な実装がこれに追随しており、
[[Perl]] が[[正規表現]]の[[事実上の標準]]の1つとみなされていました。
ただし完全な[[可搬性]]が実現されていたわけではなく、
任意の [[Perl]] の[[コード]]を埋め込めるような機能のように [[Perl]]
以外では実装不能な部分すらあります。

[60] [[正規表現]]の[[仕様]]として[[標準化団体]]等が制定したものには、
次のものがあります。

[FIG(middle list)[
- [[POSIX]]
- [[SUS]] (>>29)
- [[Unicode Regular Expressions]]
- [[ECMAScript]]
- [[C++]] (>>47)
- [[XML Schema 1.0]]
- [[XML Schema 1.1]]
- [[XQuery 1.0 and XPath 2.0 Functions and Operators]]
- [[ISO/IEC 30170]] / [[JIS X 3017]] ([[Ruby]])
- [[OpenPGP]] [SRC[>>62]]
- [CODE[SCAN]]

]FIG]

[REFS[
- [29] [CITE[Regular Expressions]] ([TIME[2014-11-07 01:09:26 +09:00]] 版) <http://pubs.opengroup.org/onlinepubs/7908799/xbd/re.html>
- [47] [CITE[Working Draft, Standard for Programming
Language C++]] ([TIME[2012-01-16 08:04:26 +09:00]] 版) <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf#page=1107>
- [62] [CITE@en[RFC 4880 - OpenPGP Message Format]] ([TIME[2016-10-16 17:19:31 +09:00]] 版) <https://tools.ietf.org/html/rfc4880#section-8>
]REFS]

;; [48] [[XQuery 1.0 and XPath 2.0 Functions and Operators]] は [[XML Schema 1.0]] 
の[[正規表現]]に多少の変更を加えています。

;; [49] [[C++]] は [[ECMAScript]] の[[正規表現]]に多少の変更を加えています [SRC[>>47]]。

;; [50] [[HTML]] は [[JavaScript]] ([[ECMAScript]]) を参照しており、
独自には定義していません。


[71] 
[[UTS #37]] は、
[[Perl 5.8]]
[[正規表現]]を参照しています。

[80] [CITE@en-US[Table Schema | Frictionless Standards]], [TIME[2023-09-28T10:34:45.000Z]], [TIME[2023-11-25T08:47:02.442Z]] <https://specs.frictionlessdata.io/table-schema/#constraints>

[[XML Schema]] 正規表現を採用。

[83] 
[[字句模型記法]] : [[POSIX正規表現]]を選択肢として提供

* [24] 正規表現の多様性

[52] [[正規表現]]は、その実装によって様々な構文や意味となっています。
あまりにも多種多様なバリエーションが存在するため、おそらくどの[[正規表現]]のバリエーションでも共通して使えるのは、
[[ASCIIラテン文字]]と[[ASCII数字]]の[[連接]]くらいでしょう。

[25] [CITE@ja-JP[正規表現メモ]] ([[KIMURA koichi]] 著, [TIME[2008-01-18 01:53:04 +09:00]] 版) <http://www.kt.rim.or.jp/~kbk/regex/regex.html>


[81] 
[CITE@ja[api-spec.pdf]], [TIME[2023-05-08T02:43:34.000Z]], [TIME[2024-04-05T13:13:11.119Z]] <https://id.ndl.go.jp/information/wp-content/uploads/2023/05/api-spec.pdf#page=18>

>
[LEFT[
[SUP[16]]SPARQL の正規表現は、XQuery/XPath の正規表現構文に従うことになっていますが、現在 Web NDLA でバックス[BR[]]
ラッシュによるエスケープ、メタ文字はサポートしていません。
]LEFT]


[84] 
[CITE[[[The TECkit Language]]]]


* 構文

[FIG(short list)[ [73] [[正規表現]]の構文
- [CODE[\p]]
]FIG]

* 文脈


[74] [CITE[[[BSON]] (Binary JSON): Specification]], [TIME[2022-04-14T15:19:45.000Z]], [TIME[2023-04-17T05:06:31.867Z]] <https://bsonspec.org/spec.html>

[75] [[BSON]] は[[正規表現]]のデータ型を定めています。

[76] >>74 には

>Regular expression - The first cstring is the regex pattern, the second is the regex options string. Options are identified by characters, which must be stored in alphabetical order. Valid options are 'i' for case insensitive matching, 'm' for multiline matching, 'x' for verbose mode, 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode ('.' matches everything), and 'u' to make \w, \W, etc. match unicode.


とあり、2つの文字列の組で表されることと、その意味の一部が例示により示されていますが、
定義はこれですべてです。

[77] [[相互運用性]]はハナから想定してない感じ?

[78] [[MongoDB Extended JSON]] はもう少し細かく定めていますが、やはり不明瞭。
しかし [[JavaScript]] の[[正規表現]]を想定してるっぽいことが書いてあります。

[82] [CITE@en[Annex B: License Matching Guidelines and Templates - specification v2.3.0]], [TIME[2024-04-23T02:59:46.000Z]], [TIME[2024-05-03T08:22:39.186Z]] <https://spdx.github.io/spdx-spec/v2.3/license-matching-guidelines-and-templates/>

> a POSIX extended regular expression (ERE) to match the replaceable text

* [22] 可視化

[18] [CITE[ブラウザで正規表現のチェックなどができるサイトまとめ | IDEA*IDEA]] ([TIME[2008-12-06 12:08:45 +09:00]] 版) <http://www.ideaxidea.com/archives/2008/12/post_964.html>

[19] [CITE[Graph::Regexp - Create graphical flowchart from a regular expression - search.cpan.org]] ([TIME[2008-12-07 09:47:47 +09:00]] 版) <http://search.cpan.org/~tels/Graph-Regexp-0.05/lib/Graph/Regexp.pm>

[[Perl]] のデバッグ出力機能を使って可視化する。

[20] >>19 なんかこれ、単純なのでも間違って出てくるんだけど、大丈夫なんだろうか?

[21] [CITE@en[Regular expression visualizer]] ([TIME[2008-12-08 14:52:29 +09:00]] 版) <https://suika.suikawiki.org/regexp/visualizer/input>

* 歴史

- [1] ''正規表現道場@2ch Part2'' <http://pc2.2ch.net/test/read.cgi/php/1038146241/>
- [3] [WEAK[2003-11-22 07:11:25 +00:00]] ''[[名無しさん]]'': ''正規表現道場@2ch Part3 s/煽り|荒らし/あぼーん/g'' <http://pc2.2ch.net/test/read.cgi/php/1069245758/>
- [4] ''正規表現'' <http://pc.2ch.net/test/read.cgi/unix/1039165754/>

[11]
[CITE[正規表現 part 3]]
<http://pc8.2ch.net/test/read.cgi/tech/1156413899/>

[35] 
>
Regular expressions provide a mechanism to select specific strings
from a set of character strings.  Regular expressions are a
language used to search for text within strings, and possibly
modify the text found with other text.  <NONE>

;; [CITE@en[RFC 3536 - Terminology Used in Internationalization in the IETF]] ([TIME[2011-01-29 02:14:52 +09:00]] 版) <http://tools.ietf.org/html/rfc3536#page-24>

[36] [[正則表現]]には言及無しwwwww


* メモ

[FIG(amazon)[
[[正規表現]]
]FIG]

- [2] [CODE(regexp)[(?:)]] ([[perl]], [[GNUEmacs]] 21+) == [CODE(regexp)[%()]] ([[Vim]])
- [5] ''contents of How to use (e|f)?grep'' <http://katsu.watanabe.name/grep/ohpcontents.html>

[6]
[CITE[Re: Character ranges in the XSD version XHTML 1.0 Strict]]
<mid:20050506.151451.108740468.mimasa@w3.org>

[[XML Schema]] の[[正規表現]]の文字範囲指定の規定の迷走
([[名無しさん]] [sage])

[7]
[CITE[Perl-compatible regular expression optimizer]] <http://bisqwit.iki.fi/source/regexopt.html>
([[名無しさん]] [WEAK[2006-01-23 00:03:50 +00:00]])



[9]
[CITE[最速インターフェース研究会 :: NamedCapture for JavaScript]] <http://la.ma.la/blog/diary_200603262310.htm>
([[名無しさん]] [WEAK[2006-03-27 23:52:31 +00:00]])

[10]
[CITE[XQuery 1.0 and XPath 2.0 Functions and Operators]] <http://www.w3.org/TR/xpath-functions/#regex-syntax>

[12]
[CITE@EN[XQuery 1.0 and XPath 2.0 Functions and Operators]] ([CODE[2007-01-19 07:02:42 +09:00]] 版) <http://www.w3.org/TR/2007/REC-xpath-functions-20070123/#regex-syntax>
([[名無しさん]])

[13]
[CITE[Bug 330352 &#8211; Very non-greedy regexp causes crash in jsregexp.c]] ([CODE[2007-02-13 23:10:53 +09:00]] 版) <https://bugzilla.mozilla.org/show_bug.cgi?id=330352>
([[名無しさん]] [WEAK[2007-02-13 14:21:45 +00:00]])

[14]
[CITE[Bug 330352 &#8211; Very non-greedy regexp causes crash in jsregexp.c]] ([CODE[2007-02-13 23:10:53 +09:00]] 版) <https://bugzilla.mozilla.org/show_bug.cgi?id=330352>
([[名無しさん]] [WEAK[2007-02-13 14:26:25 +00:00]])

[15]
[CITE@en-us[SRX Specification]] ([CODE[2007-02-24 17:26:17 +09:00]] 版) <http://www.lisa.org/standards/srx/srx.html#Intro_RegExp>
([[名無しさん]])

[16]
[CITE@ja[Shibuya.js in Kyoto 発表資料「取説 正規表現」: Days on the Moon]] ([CODE[2008-07-26 22:51:06 +09:00]] 版) <http://nanto.asablo.jp/blog/2008/07/21/3640937>
([[名無しさん]])

[17]
[CITE[ECMAScript デス 3]]
<http://pc11.2ch.net/test/read.cgi/tech/1190160481/432>

>
'aaaggfeeeehhh'.split(/(?=(.)\1*)/);
>
-IE6:    a,a,a,g,g,f,e,e,e,e,h,h,h
-Firefox3: a,a,a,a,a,g,g,g,g,f,f,e,e,e,e,e,e,e,e,h,h,h,h,h,h,h
-Chrome: a,a,a,a,a,g,g,g,g,f,f,e,e,e,e,e,e,e,e,h,h,h,h,h,h

[26] [CITE@ja[Lesson1 Step2 検索条件を設定しよう ~簡易表現と正規表現~ - 超漢字ウェブサイト]] ([TIME[2008-07-09 11:32:56 +09:00]] 版) <http://www.chokanji.com/knowhow/vol01_02.html>

[27] >>26 で特徴的なのは:

>
,&TNNNNNN;  	,1文字を[[テキスト形式TRONコード]]で表記します。
>
,[:名前:]  	,あらかじめ名前の決まっている文字クラスを指定します。日本基本、日本補助、GT、大漢和、中国、韓国、台湾、各国、JIS第1第2、JIS第3、JIS第4が使用できます。(検索文字列)  '''['''[:中国:]''']'''

[28] [[RFC 5444]] は [[SUS]] を参照しつつ[[正規表現]]風味の [[BNF]]
を独自に定義し、それを用いて書式を定義しています。

[30] [CITE[正規表現の文字クラスまとめ - 名もないテクノ手]] ([TIME[2009-03-09 22:39:19 +09:00]] 版) <http://d.hatena.ne.jp/seuzo/20090309/1236525090>

[31] >>30 は [[InDesign]]。ほぼ [[Perl]] と同じかな。 [CODE(regexp)[[['''[''':unicode:''']''']]]]
なんてのがあるんだ。

[32] [CITE[正規表現で素数判定 - NO!と言えるようになりたい]]
([TIME[2010-07-24 13:51:10 +09:00]] 版)
<http://d.hatena.ne.jp/ytakano/20100721/1279736214>

[33] [CITE@EN[XQuery 1.0 and XPath 2.0 Functions and Operators (Second Edition)]]
( ([TIME[2010-12-17 00:06:54 +09:00]] 版))
<http://www.w3.org/TR/2010/REC-xpath-functions-20101214/#regex-syntax>

[34] [CITE@en-US[Efficient XML Interchange (EXI) Format 1.0]]
( ([TIME[2011-03-10 23:00:15 +09:00]] 版))
<http://www.w3.org/TR/2011/REC-exi-20110310/#regexToCharset>

[37] [CITE@EN[W3C XML Schema Definition Language (XSD) 1.1 Part 2: Datatypes]]
( ([TIME[2012-04-05 06:34:51 +09:00]] 版))
<http://www.w3.org/TR/2012/REC-xmlschema11-2-20120405/#regexs>

[38] [CITE@EN[SPARQL 1.1 Query Language]]
( ([TIME[2013-03-21 20:27:53 +09:00]] 版))
<http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#func-regex>

[40] [CITE@EN[XPath and XQuery Functions and Operators 3.0]]
( ([TIME[2014-04-08 07:02:07 +09:00]] 版))
<http://www.w3.org/TR/xpath-functions-3/#regex-syntax>

[41] [CITE@en-US[Internationalization Tag Set (ITS) Version 2.0]]
( ([TIME[2013-10-27 19:39:43 +09:00]] 版))
<http://www.w3.org/TR/its20/#allowedchars>

[42] [CITE@ja[Emacs Lisp]]
( ([TIME[2014-06-17 06:23:25 +09:00]] 版))
<http://www.mew.org/~kazu/doc/elisp/regexp.html>

[43] [CITE@ja[K.Takata's software: bregonig.dll]]
( ([TIME[2014-09-18 21:57:55 +09:00]] 版))
<http://homepage3.nifty.com/k-takata/mysoft/bregonig.html>

[44] [CITE@en[k-takata/Onigmo]]
( ([TIME[2014-11-07 00:58:41 +09:00]] 版))
<https://github.com/k-takata/Onigmo>

[45] [CITE[Oniguruma]]
( ([TIME[2014-11-07 00:58:38 +09:00]] 版))
<http://www.geocities.jp/kosako3/oniguruma/>

[46] [CITE[Pattern (Java Platform SE 6)]]
( ([TIME[2011-12-02 14:55:51 +09:00]] 版))
<http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html>

[FIG(quote)[
[FIGCAPTION[
[53] [CITE[OASIS Open Document Format for Office Applications (OpenDocument) Version 1.2 - Part 1: OpenDocument Schema]]
([TIME[2011-09-29 13:00:00 +09:00]] 版)
<http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#a19_743table_use-regular-expressions>
]FIGCAPTION]

> Regular expressions are implementation-dependent expressions that, at a minimum, conform to the requirements of Conformance Clause C1 of '''['''UTR18''']'''.

]FIG]


[54] [CITE@en[regex - Rust]]
( ([TIME[2015-02-04 22:01:13 +09:00]] 版))
<http://doc.rust-lang.org/regex/regex/index.html>

[55] [CITE@en[RFC 2440 - OpenPGP Message Format]]
([TIME[2015-02-15 14:39:41 +09:00]] 版)
<https://tools.ietf.org/html/rfc2440#section-8>

[56] [CITE@en[RFC 4880 - OpenPGP Message Format]]
([TIME[2015-04-05 14:41:03 +09:00]] 版)
<https://tools.ietf.org/html/rfc4880#section-8>

[FIG(quote)[
[FIGCAPTION[
[58] [CITE@en[Model for Tabular Data and Metadata on the Web]]
([TIME[2015-12-03 19:28:42 +09:00]] 版)
<http://www.w3.org/TR/2015/REC-tabular-data-model-20151217/#h-formats-for-durations>
]FIGCAPTION]

> If the datatype base is a duration type, the datatype format annotation provides a regular expression for the string values, with syntax and processing defined by '''['''ECMASCRIPT''']'''. If the supplied value is not a valid regular expression, implementations must issue a warning and proceed as if no format had been provided.

]FIG]


[FIG(quote)[
[FIGCAPTION[
[59] [CITE@en[Model for Tabular Data and Metadata on the Web]]
([TIME[2015-12-03 19:28:42 +09:00]] 版)
<http://www.w3.org/TR/2015/REC-tabular-data-model-20151217/#h-formats-for-other-types>
]FIGCAPTION]

> If the datatype base is not numeric, boolean, a date/time type, or a duration type, the datatype format annotation provides a regular expression for the string values, with syntax and processing defined by '''['''ECMASCRIPT''']'''. If the supplied value is not a valid regular expression, implementations must issue a warning and proceed as if no format had been provided.

]FIG]

[63] [[POSIX]] の[[正規表現]]は[[字句模型記法]]としても使うことができます。

[64] [CITE@EN[XPath and XQuery Functions and Operators 3.1]]
([TIME[2017-03-21 16:02:06 +09:00]])
<https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#string.match>

[65] [CITE@en[RFC 2168 - Resolution of Uniform Resource Identifiers using the Domain Name System]]
([TIME[2017-04-23 19:27:05 +09:00]])
<https://tools.ietf.org/html/rfc2168#page-13>

[66] [CITE@en[RFC 2915 - The Naming Authority Pointer (NAPTR) DNS Resource Record]]
([TIME[2017-04-16 19:40:24 +09:00]])
<https://tools.ietf.org/html/rfc2915#section-3>

[67] [CITE@en[544123 - Google Chrome Helper consumes 100% of CPU with the following regular expression - chromium - Monorail]]
( ([TIME[2017-05-02 16:04:03 +09:00]]))
<https://bugs.chromium.org/p/chromium/issues/detail?id=544123>

[68] [CITE@EN[XSL Transformations (XSLT) Version 3.0]]
([TIME[2017-06-06 21:59:56 +09:00]])
<https://www.w3.org/TR/2017/REC-xslt-30-20170608/#regular-expressions>

[FIG(quote)[
[FIGCAPTION[
[69] [CITE@en[Concise Binary Object Representation (CBOR)]]
([TIME[2018-02-23 07:30:37 +09:00]])
<https://cbor-wg.github.io/CBORbis/#encodedtext>
]FIGCAPTION]

> Tag 35 is for regular expressions in Perl Compatible Regular Expressions (PCRE) / JavaScript syntax '''['''ECMA262''']'''.

]FIG]


[70] [CITE@en[Syntax · google/re2 Wiki]]
([TIME[2019-03-19 14:50:43 +09:00]])
<https://github.com/google/re2/wiki/Syntax>

[72] [CITE@ja[大名 力 (Tsutomu Ohna) - マイポータル - researchmap]], [[Japan Science and Technology Agency]], [TIME[2021-10-12T07:23:58.000Z]] <https://researchmap.jp/multidatabases/multidatabase_contents/detail/286001/380b009ff5d2d26cfb2321ae7b8b9d2f>

[79] [CITE@en[wakaba/perl-regexp-utils: Perl modules for regular expressions]], [TIME[2023-04-18T08:52:28.000Z]] <https://github.com/wakaba/perl-regexp-utils>
