Method: Numerals::Format::Symbols#regexp

Defined in:
lib/numerals/format/symbols.rb

#regexp(*args) ⇒ Object

Generate a regular expression to match any of the passed symbols.

symbols.regexp(:nan, :plus, :minus) #=> "(NaN|+|-)"

The special symbol :digits can also be passed to generate all the digits, in which case the :base option can be used to generate digits only for some base smaller than the maximum defined for digits.

symbols.regexp(:digits, :point, base: 10) # => "(0|1|...|9)"

The option :no_capture can be used to avoid generating a capturing group; otherwise the result is captured group (surrounded by parenthesis)

symbols.regexp(:digits, no_capture: true) # => "(?:...)"

The :case_sensitivity option is used to generate a regular expression that matches the case of the text as defined by ghe case_sensitive attribute of the Symbols. If this option is used the result should not be used in a case-insensitive regular expression (/…/i).

/#{symbols.regexp(:digits, case_sensitivity: true)}/

If the options is not used, than the regular expression should be be made case-insensitive according to the Symbols:

if symbols.case_sensitive?
  /#{symbols.regexp(:digits)}/

else

/#{symbols.regexp(:digits)}/i


343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/numerals/format/symbols.rb', line 343

def regexp(*args)
  options = args.pop if args.last.is_a?(Hash)
  options ||= {}
  symbols = args
  digits = symbols.delete(:digits)
  grouped_digits = symbols.delete(:grouped_digits)
  symbols = symbols.map { |s|
    s.is_a?(Symbol) ? send(s)  : s
  }
  if grouped_digits
    symbols += [group_separator, insignificant_digit]
  elsif digits
    symbols += [insignificant_digit]
  end
  if digits || grouped_digits
    symbols += @digits.digits(options)
  end
  regexp_group(symbols, options)
end