summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-05-04 12:32:43 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-05-04 12:32:43 +0900
commit18b27185c37378c65fee12091c5ff90d5be2ec97 (patch)
tree171561dada4447b3f5645e1c96a3e9486bf97ac5 /doc
parent04ee666aab071cb6fd9a8b356b22d73250ca6b9b (diff)
downloadruby-18b27185c37378c65fee12091c5ff90d5be2ec97.tar.gz
[DOC] regexp absent operator
Diffstat (limited to 'doc')
-rw-r--r--doc/regexp.rdoc17
1 files changed, 17 insertions, 0 deletions
diff --git a/doc/regexp.rdoc b/doc/regexp.rdoc
index 51ea772062..b9c89b1c86 100644
--- a/doc/regexp.rdoc
+++ b/doc/regexp.rdoc
@@ -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