[DOC] regexp absent operator

This commit is contained in:
Nobuyoshi Nakada 2023-05-04 12:32:43 +09:00
parent 04ee666aab
commit 18b27185c3
No known key found for this signature in database
GPG Key ID: 7CD2805BFA3770C6
Notes: git 2023-05-04 03:44:36 +00:00

View File

@ -602,6 +602,23 @@ text appearing in <b></b> tags without including the tags in the match:
/(?<=<b>)\w+(?=<\/b>)/.match("Fortune favours the <b>bold</b>")
#=> #<MatchData "bold">
== Absent operator
Absent operator <tt>(?~</tt><i>pat</i><tt>)</tt> matches string which does
not match <i>pat</i>.
For example, a regexp to match C comment, which is enclosed by <tt>/*</tt>
and <tt>*/</tt> and does not include <tt>*/</tt>, using absent operator:
%r[/\*(?~\*/)\*/] =~ "/* comment */ not-comment */"
#=> #<MatchData "/* comment */">
This is often shorter and clearer than without absent operator:
%r[/\*[^\*]*\*+(?:[^\*/][^\*]*\*+)*/]
%r[/\*(?:(?!\*/).)*\*/]
%r[/\*(?>.*?\*/)]
== Options
The end delimiter for a regexp can be followed by one or more single-letter