Skip to content

Commit

Permalink
[DOC] regexp absent operator
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed May 4, 2023
1 parent 04ee666 commit 18b2718
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions doc/regexp.rdoc
Expand Up @@ -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
Expand Down

0 comments on commit 18b2718

Please sign in to comment.