Code tagged with regex
validates_format_of :company_url,
:with => /((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?)/
This still needs some polishing, but useful nonetheless. The original version of these regexes came from RubyGarden.
class String
NUM = /[-+]?(\d+\.)?\d+([eE][-+]?\d+)?/
DEC = /[-+]?(0d\d([\d_]*\d)?)|([1-9]([\d_]*\d)?)|0/
BIN = /[-+]?[01]([01_]*[01])?/
OCT = /[-+]?0([0-7_]*[0-7])?/
HEX = /[-+]?0x[\da-fA-F]([\da-fA-F_]*[\da-fA-F])?/
def number?; (self =~ NUM) != nil; end
def decimal?; (self =~ DEC) != nil; end
def binary?; (self =~ BIN) != nil; end
def octal?; (self =~ OCT) != nil; end
def hex?; (self =~ HEX) != nil; end
end
.NET Regular Expressions allow you to embed comments and whitespace within pattern strings making them clearer to read (because of the whitespace), and easier to maintain (due to the modularity).
To use this feature you must turn on the RegexOptions.IgnorePatternWhitespace flag. This can be achieved either by using the RegexOptions enumerated datatype or, via the (?x) mode modifier. Therefore the following two examples are functionally identical:
Regex re = new Regex(
@" # This pattern matches Foo
(?i) # turn on insensitivity
# The Foo bit
\b(Foo)\b "
, RegexOptions.IgnorePatternWhitespace ) ;
Regex re = new Regex(
@"(?x)
# This pattern matches Foo
(?i) # turn on insensitivity
# The Foo bit
\b(Foo)\b "
) ;