diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2023-05-04 12:32:43 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2023-05-04 12:32:43 +0900 |
commit | 18b27185c37378c65fee12091c5ff90d5be2ec97 (patch) | |
tree | 171561dada4447b3f5645e1c96a3e9486bf97ac5 /doc | |
parent | 04ee666aab071cb6fd9a8b356b22d73250ca6b9b (diff) | |
download | ruby-18b27185c37378c65fee12091c5ff90d5be2ec97.tar.gz |
[DOC] regexp absent operator
Diffstat (limited to 'doc')
-rw-r--r-- | doc/regexp.rdoc | 17 |
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 |