Class: CommonRegex

Inherits:
Object
  • Object
show all
Defined in:
lib/commonregex.rb

Constant Summary collapse

@@date_regex =
Regexp.new('(' + CommonRegex.group(
  CommonRegex.any(
    [
      day_regex + '\\s+(?:of\\s+)?' + month_regex,
      month_regex + '\\s+' + day_regex
    ]
  )
) + '(?:\\,)?\\s*' + CommonRegex.opt(year_regex) + '|[0-3]?\\d[-/][0-3]?\\d[-/]\\d{2,4})', Regexp::IGNORECASE || Regexp::MULTILINE)
@@time_regex =
/\b((0?[0-9]|1[0-2])(:[0-5][0-9])?(am|pm)|([01]?[0-9]|2[0-3]):[0-5][0-9])/im
@@phone_regex =
/(\d?[^\s\w]*(?:\(?\d{3}\)?\W*)?\d{3}\W*\d{4})/im
/((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*@\)|[^\s`!()\[\]{};:\'".,<>?]))/im
@@emails_regex =
/([a-z0-9!#$%&'*+\/=?\^_`{|}~\-]+@([a-z0-9]+\.)+([a-z0-9]+))/im
@@ipv4_regex =
/\b(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\b/m
@@ipv6_regex =
/((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]@{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))\b/im
@@hex_colors_regex =
/(#(?:[0-9a-fA-F]{3}){1,2})\b/im
@@acronyms_regex =
/\b(([A-Z]\.)+|([A-Z]){2,})/m
@@money_regex =
/(((^|\b)US?)?\$\s?[0-9]{1,3}((,[0-9]{3})+|([0-9]{3})+)?(\.[0-9]{1,2})?\b)/m
@@percentage_regex =
/((100(\.0+)?|[0-9]{1,2}(\.[0-9]+)?)%)/m
@@credit_card_regex =
/((?:(?:\d{4}[- ]){3}\d{4}|\d{16}))(?![\d])/m
@@address_regex =
/(\d{1,4} [\w\s]{1,20}(?:(street|avenue|road|highway|square|traill|drive|court|parkway|boulevard)\b|(st|ave|rd|hwy|sq|trl|dr|ct|pkwy|blvd)\.(?=\b)?))/im

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = '') ⇒ CommonRegex

Returns a new instance of CommonRegex.



43
44
45
# File 'lib/commonregex.rb', line 43

def initialize(text = '')
  @text = text;
end

Class Method Details

.any(regexes) ⇒ Object



12
13
14
# File 'lib/commonregex.rb', line 12

def self.any(regexes)
  regexes.join('|')
end

.group(regex) ⇒ Object



8
9
10
# File 'lib/commonregex.rb', line 8

def self.group(regex)
  '(?:' + regex + ')'
end

.opt(regex) ⇒ Object

Methods used to generate @date_regex



4
5
6
# File 'lib/commonregex.rb', line 4

def self.opt (regex)
  '(?:' + regex + ')?'
end

Instance Method Details

#get_acronyms(text = @text) ⇒ Object



79
80
81
# File 'lib/commonregex.rb', line 79

def get_acronyms(text = @text)
  get_matches(text, @@acronyms_regex)
end

#get_addresses(text = @text) ⇒ Object



95
96
97
# File 'lib/commonregex.rb', line 95

def get_addresses(text = @text)
  get_matches(text, @@address_regex)
end

#get_credit_cards(text = @text) ⇒ Object



91
92
93
# File 'lib/commonregex.rb', line 91

def get_credit_cards(text = @text)
  get_matches(text, @@credit_card_regex)
end

#get_dates(text = @text) ⇒ Object



47
48
49
# File 'lib/commonregex.rb', line 47

def get_dates(text = @text)
  get_matches(text, @@date_regex)
end

#get_emails(text = @text) ⇒ Object



63
64
65
# File 'lib/commonregex.rb', line 63

def get_emails(text = @text)
  get_matches(text, @@emails_regex)
end

#get_hex_colors(text = @text) ⇒ Object



75
76
77
# File 'lib/commonregex.rb', line 75

def get_hex_colors(text = @text)
  get_matches(text, @@hex_colors_regex)
end

#get_ipv4(text = @text) ⇒ Object



67
68
69
# File 'lib/commonregex.rb', line 67

def get_ipv4(text = @text)
  get_matches(text, @@ipv4_regex)
end

#get_ipv6(text = @text) ⇒ Object



71
72
73
# File 'lib/commonregex.rb', line 71

def get_ipv6(text = @text)
  get_matches(text, @@ipv6_regex)
end


59
60
61
# File 'lib/commonregex.rb', line 59

def get_links(text = @text)
  get_matches(text, @@links_regex)
end

#get_money(text = @text) ⇒ Object



83
84
85
# File 'lib/commonregex.rb', line 83

def get_money(text = @text)
  get_matches(text, @@money_regex)
end

#get_percentages(text = @text) ⇒ Object



87
88
89
# File 'lib/commonregex.rb', line 87

def get_percentages(text = @text)
  get_matches(text, @@percentage_regex)
end

#get_phones(text = @text) ⇒ Object



55
56
57
# File 'lib/commonregex.rb', line 55

def get_phones(text = @text)
  get_matches(text, @@phone_regex)
end

#get_times(text = @text) ⇒ Object



51
52
53
# File 'lib/commonregex.rb', line 51

def get_times(text = @text)
  get_matches(text, @@time_regex)
end