From 18b27185c37378c65fee12091c5ff90d5be2ec97 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Thu, 4 May 2023 12:32:43 +0900 Subject: [PATCH] [DOC] regexp absent operator --- doc/regexp.rdoc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 tags without including the tags in the match: /(?<=)\w+(?=<\/b>)/.match("Fortune favours the bold") #=> # +== Absent operator + +Absent operator (?~pat) matches string which does +not match pat. + +For example, a regexp to match C comment, which is enclosed by /* +and */ and does not include */, using absent operator: + + %r[/\*(?~\*/)\*/] =~ "/* comment */ not-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