Class: Regexp

Inherits:
Object show all
Defined in:
lib/rubyhacks.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_escaped(*args) ⇒ Object



614
615
616
# File 'lib/rubyhacks.rb', line 614

def self.new_escaped(*args)
	new(escape(*args))
end

.properly_nested(left_delim, right_delim, start_anywhere = true, ext = "") ⇒ Object

Raises:

  • (ArgumentError)


601
602
603
604
605
606
607
# File 'lib/rubyhacks.rb', line 601

def self.properly_nested(left_delim, right_delim, start_anywhere=true, ext = "")
	raise ArgumentError.new("Left and right delimiters (#{left_delim.inspect}, #{right_delim.inspect}) cannot be the same") if left_delim == right_delim
	
	nodelim = "(?<nodelim#{ext}>\\\\\\\\|\\\\\\#{left_delim}|\\\\\\#{right_delim}|[^#{left_delim}#{right_delim}\\\\]|\\\\[^#{left_delim}#{right_delim}\\\\])"
	
	new("#{start_anywhere ? "(?<before#{ext}>#{nodelim}*?)" : ""}(?<group#{ext}>\\#{left_delim}#{start_anywhere ? "\\g<nodelim#{ext}>" : nodelim}*?(?:\\g<group#{ext}>\\g<nodelim#{ext}>*?)*\\#{right_delim})")
end

.quoted_stringObject



608
609
610
# File 'lib/rubyhacks.rb', line 608

def self.quoted_string
	/"(?:\\\\|\\"|[^"\\]|\\[^"\\])*"|'(?:\\\\|\\'|[^'\\]|\\[^'\\])*'/
end

.whole_string(regex) ⇒ Object



611
612
613
# File 'lib/rubyhacks.rb', line 611

def self.whole_string(regex)
	new("\\A#{regex.to_s}\\Z")
end

Instance Method Details

#verbatimObject



598
599
600
# File 'lib/rubyhacks.rb', line 598

def verbatim
	self.inspect.sub(/\A\//,'').sub(/\/[mix]*\Z/,'')
end