Module: Re::ConstructionMethods

Defined in:
lib/re.rb

Overview

This module defines a number of methods returning common pre-packaged regular expressions along with methods to create regular expressions from character classes and other objects. ConstructionMethods is mixed into the NULL Rexp object so that re() without arguments can be used to access the methods.

Constant Summary collapse

ANY_CHAR =
Rexp.raw(".")

Instance Method Summary collapse

Instance Method Details

#any(*chars) ⇒ Object

:call-seq:

re.any
re.any(chars)
re.any(range)
re.any(chars, range, ...)

Regular expression that matches a character from a character class.

Any without any arguments will match any single character. Any with one or more arguments will construct a character class for the arguments. If the argument is a three character string where the middle character is “-”, then the argument represents a range of characters. Otherwise the arguments are treated as a list of characters to be added to the character class.

Examples:

re.any                            -- matches any character
re.any("aieouy")                  -- matches any vowel
re.any("0-9")                     -- matches any digit
re.any("A-Z", "a-z", "0-9")       -- matches any alphanumeric character
re.any("A-Z", "a-z", "0-9", "_")  -- matches any alphanumeric character
                                     or an underscore


540
541
542
543
544
545
546
# File 'lib/re.rb', line 540

def any(*chars)
  if chars.empty?
    ANY_CHAR
  else
    Rexp.new("[" + char_class(chars)  + "]", GROUPED, [])
  end
end

#breakObject

:call-seq:

re.break

Regular expression that matches any break between word/non-word characters. This is a zero length match. (equivalent to /\b/)



645
646
647
# File 'lib/re.rb', line 645

def break
  @break ||= Rexp.raw("\\b")
end

#digitObject

:call-seq:

re.digit

Regular expression that matches a single digit. (equivalent to /\d/)



654
655
656
# File 'lib/re.rb', line 654

def digit
  @digit ||= Rexp.raw("\\d")
end

#digitsObject

:call-seq:

re.digits

Regular expression that matches a sequence of digits. (equivalent to /\d+/)



663
664
665
# File 'lib/re.rb', line 663

def digits
  @digits ||= digit.one_or_more
end

#hex_digitObject

:call-seq:

re.hex_digit

Regular expression that matches a single hex digit. (equivalent to /[A-Fa-f0-9]/)



672
673
674
# File 'lib/re.rb', line 672

def hex_digit
  @hex_digit ||= any("0-9", "a-f", "A-F")
end

#hex_digitsObject

:call-seq:

re.hex_digits

Regular expression that matches a sequence of hex digits (equivalent to /[A-Fa-f0-9]+/)



681
682
683
# File 'lib/re.rb', line 681

def hex_digits
  @hex_digits ||= hex_digit.one_or_more
end

#none(*chars) ⇒ Object

:call-seq:

re.none(chars)
re.none(range)
re.none(chars, range, ...)

Regular expression that matches a character not in a character class.

None with one or more arguments will construct a character class for the given arguments. If the argument is a three character string where the middle character is “-”, then the argument represents a range of characters. Otherwise the arguments are treated as a list of characters to be added to the character class.

Examples:

re.none("aieouy")                 -- matches non-vowels
re.none("0-9")                    -- matches non-digits
re.none("A-Z", "a-z", "0-9")      -- matches non-alphanumerics


569
570
571
# File 'lib/re.rb', line 569

def none(*chars)
  Rexp.new("[^" + char_class(chars)  + "]", GROUPED, [])
end

#nonspaceObject

:call-seq:

re.nonspace

Regular expression that matches any non-white space character. (equivalent to /S/)



609
610
611
# File 'lib/re.rb', line 609

def nonspace
  @nonspace ||= Rexp.raw("\\S")
end

#nonspacesObject

:call-seq:

re.nonspaces

Regular expression that matches any sequence of non-white space characters. (equivalent to /S+/)



618
619
620
# File 'lib/re.rb', line 618

def nonspaces
  @nonspaces ||= Rexp.raw("\\S").one_or_more
end

#nullObject

:call-seq:

re.null

Regular expression that matches the null string



510
511
512
# File 'lib/re.rb', line 510

def null
  self
end

#spaceObject

:call-seq:

re.space

Regular expression that matches any white space character. (equivalent to /\s/)



591
592
593
# File 'lib/re.rb', line 591

def space
  @space ||= Rexp.raw("\\s")
end

#spacesObject

:call-seq:

re.spaces

Regular expression that matches any sequence of white space characters. (equivalent to /\s+/)



600
601
602
# File 'lib/re.rb', line 600

def spaces
  @spaces ||= space.one_or_more
end

#wordObject

:call-seq:

re.word

Regular expression that matches any sequence of word characters. (equivalent to /\w+/)



636
637
638
# File 'lib/re.rb', line 636

def word
  @word ||= word_char.one_or_more
end

#word_charObject

:call-seq:

re.word_char

Regular expression that matches any word character. (equivalent to /\w/)



627
628
629
# File 'lib/re.rb', line 627

def word_char
  @word_char ||= Rexp.raw("\\w")
end