Skip to content
Tcl Source Code Artifact [8dbe346221]
Login

Artifact [8dbe346221]

Artifact 8dbe3462215f63026f87a4929af246bcd499cd94c32c73732e1cd8eaff7f2e15:

Ticket change [8dbe346221] - New ticket [fbc56b259e989230|fbc56b259e] <i>Add support for \z in regex as end-of-string anchor</i>. by david-a-wheeler on 2025-05-05 18:34:36.
D 2025-05-05T18:34:36.198
J assignee nobody
J closer nobody
J cmimetype text/plain
J comment I\spropose\sthat\s\\z\sbe\saccepted\sin\sregular\sexpressions\sas\sa\ssynonym\sfor\s\\Z.\r\nBelow\sis\sa\srationale,\sthe\sone-line\spatch\sthat\simplements\sit,\sand\sa\r\nfew\smore\spatch\slines\sto\smodify\sthe\sdocumentation.\r\n\r\nFirst,\sthe\srationale.\r\n\r\nA\scommon\suse\sfor\sregexes\sin\ssecurity\sis\sto\scheck\sinputs,\r\nwhich\sneeds\sanchors\sfor\s"beginning\sof\sstring"\sand\s"end\sof\sstring"\r\n(regardless\sof\smulti-line\smode,\setc.)..\r\nDifferent\sregex\sengines\snotate\sthis\sdifferently,\smaking\sguidance\scomplicated.\r\nSee:\shttps://best.openssf.org/Correctly-Using-Regular-Expressions.html\r\n\r\nPeople\soften\scopy\sregular\sexpressions\sfrom\sone\sengine\sto\sanother.\r\n46%\sof\sdevelopers\sincorrectly\sbelieve\sthat\sregexes\sare\sthe\ssame\severywhere\r\nper\sWang\set\sal’s\s“An\sEmpirical\sStudy\son\sRegular\sExpression\sBugs”\s(2020).\r\nIt's\sgoing\sto\sget\s*worse*\swith\sLLMs,\swhich\sdon't\salways\snote\scontext.\r\n\r\nThe\sgood\snews\sis\sthat\sthere\s*is*\sa\snotation\severyone\scan\ssupport:\s\\A\s...\s\\z.\r\nThis\sis\ssupported\sby:\r\nJava,\s.NET/C#,\sPerl,\sPCRE,\sPHP\s(using\sPCRE),\sRuby,\r\nRE2\swhich\sis\swidely\sused\sby\sGo,\sand\sRust\scrate\sregex\r\nwhich\sis\swidely\sused\sby\sRust.\r\n\r\nThe\sphrase\s"\\Z"\scannot\sbecome\sa\sgenerally-agreed\ssymbol\sfor\send-of-string,\r\nbecause\sit's\salready\sused\sfor\s"optional\snewline\sfollowed\sby\send\sof\sstring"\son\r\nmany\splatforms,\sincluding\sJava,\s.NET/C#,\sPerl,\sPCRE\s,\sPHP\s(using\sPCRE),\r\nand\sRuby.\sCitations:\r\nhttps://learn.microsoft.com/en-us/dotnet/standard/base-types/anchors-in-regular-expressions#end-of-string-or-before-ending-newline-z\r\nhttps://www.pcre.org/original/doc/html/pcrepattern.html#SEC5\r\nhttps://ruby-doc.org/core-2.5.8/Regexp.html\r\n\r\nThe\sAustin\sGroup\shas\sdecided\sto\sadd\s\\A\sand\s\\z\s(not\s\\Z)\sto\r\nPOSIX\sEREs\sand\sencourage\sthem\sfor\sPOSIX\sBREs:\r\nhttps://www.austingroupbugs.net/view.php?id=1919\r\n\r\nPython\salready\ssupports\s\\A;\sthey've\sdecided\sto\sadd\ssupport\sfor\s\\z\r\nfor\s"end-of-string"\sand\sare\sdiscussing\sdeprecating\s\\Z\sfor\send-of-string.\r\nhttps://github.com/python/cpython/issues/133306\r\n\r\nAnyway,\sI\shope\sthat\s\\A\s...\s\\z\swould\sbe\ssupported\shere\sas\swell.\r\nIt'd\sbe\snice\sto\sbe\sable\sto\ssay\s"this\sworks\spractically\severywhere"\r\ninstead\sof\sthe\scomplicated\sexplanations\sthat\smust\sbe\sdone\stoday.\r\n\r\nThanks!\r\n\r\n\r\ndiff\s--git\sa/generic/regc_lex.c\sb/generic/regc_lex.c\r\nindex\sbf936ca607..cf791bacf1\s100644\r\n---\sa/generic/regc_lex.c\r\n+++\sb/generic/regc_lex.c\r\n@@\s-872,6\s+872,7\s@@\slexescape(\r\n\s\tNOTE(REG_ULOCALE);\r\n\s\tRETV(NWBDRY,\s0);\r\n\s\tbreak;\r\n+\s\s\s\scase\sCHR('z'):\r\n\s\s\s\s\scase\sCHR('Z'):\r\n\s\tRETV(SEND,\s0);\r\n\s\tbreak;\r\ndiff\s--git\sa/doc/re_syntax.n\sb/doc/re_syntax.n\r\nindex\sb2143493a1..c1c8971e53\s100644\r\n---\sa/doc/re_syntax.n\r\n+++\sb/doc/re_syntax.n\r\n@@\s-136,6\s+136,7\s@@\sbelow).\r\n\s.PP\r\n\sThe\sdifference\sbetween\sstring\sand\sline\smatching\smodes\sis\simmaterial\r\n\swhen\sthe\sstring\sdoes\snot\scontain\sa\snewline\scharacter.\s\sThe\s\\fB\\eA\\fR\r\n+and\s\\fB\\ez\\fR\sand\r\n\sand\s\\fB\\eZ\\fR\sconstraint\sescapes\shave\sa\ssimilar\spurpose\sbut\sare\r\n\salways\sconstraints\sfor\sthe\soverall\sstring.\r\n\s.PP\r\n@@\s-452,6\s+453,9\s@@\smatches\sonly\sat\sthe\send\sof\sa\sword\r\n\smatches\sonly\sat\sthe\sbeginning\sor\send\sof\sa\sword\r\n\s.IP\s\\fB\\eY\\fR\s6\r\n\smatches\sonly\sat\sa\spoint\sthat\sis\snot\sthe\sbeginning\sor\send\sof\sa\sword\r\n+.IP\s\\fB\\ez\\fR\s6\r\n+matches\sonly\sat\sthe\send\sof\sthe\sstring\s(see\s\\fBMATCHING\\fR,\sbelow,\sfor\r\n+how\sthis\sdiffers\sfrom\r\n\s.IP\s\\fB\\eZ\\fR\s6\r\n\smatches\sonly\sat\sthe\send\sof\sthe\sstring\s(see\s\\fBMATCHING\\fR,\sbelow,\sfor\r\n\show\sthis\sdiffers\sfrom\r\n@@\s-707,7\s+711,8\s@@\sexpressions\susing\s\\fB^\\fR\swill\snever\smatch\sthe\snewline\scharacter\s(so\r\n\sthat\smatches\swill\snever\scross\snewlines\sunless\sthe\sRE\sexplicitly\r\n\sarranges\sit)\sand\s\\fB^\\fR\sand\s\\fB$\\fR\swill\smatch\sthe\sempty\sstring\safter\r\n\sand\sbefore\sa\snewline\srespectively,\sin\saddition\sto\smatching\sat\r\n-beginning\sand\send\sof\sstring\srespectively.\sARE\s\\fB\\eA\\fR\sand\s\\fB\\eZ\\fR\r\n+beginning\sand\send\sof\sstring\srespectively.\sARE\s\\fB\\eA\\fR\sand\r\n+\\fB\\ez\\fR\sand\s\\fB\\eZ\\fR\r\n\scontinue\sto\smatch\sbeginning\sor\send\sof\sstring\s\\fIonly\\fR.\r\n\s.PP\r\n\sIf\spartial\snewline-sensitive\smatching\sis\sspecified,\sthis\saffects
J is_private 0
J login david-a-wheeler
J priority 5\sMedium
J private_contact 4142338a69292517b14056e8c2f582f9f21d882c
J resolution None
J severity Important
J status Open
J submitter david-a-wheeler
J subsystem 43.\sRegexp
J title Add\ssupport\sfor\s\\z\sin\sregex\sas\send-of-string\sanchor
J type Patch
K fbc56b259e989230e54a4053feeecf7aa765f61d
U david-a-wheeler
Z 62ffb3439d83bdbd54052197cf55a301