Regular Expression "Empty Range in Char Class Error"

Error empty range in char class when using var in regex

The below would generate a hash of domain as the key and array of matching emails

@domains.each_with_object({}) { |domain,hash| hash[domain] = emails.select { |email| email.ends_with(domain) } }

Premature end of char class in interpolated regex

Is this an indication that something unanticipated is being fed into the array as a string?

Yes, that is it exactly. I expect that you have nested arrays and somewhere in there you have an array of an empty array [[]] whose to_s representation produces the result you found.

When you use interpolation in a regex literal the characters in your source are treated as they would be in regex. Just as /b[/ is not a valid regular expression, so foo="b["; bar=/#{foo}/ is not valid.

nilfacs = [ "a[]", "b[", "c]", [[]] ]

nilfacs.each do |fac|
begin
p /#{fac}/
rescue RegexpError=>e
puts e
end
end

#=> empty char-class: /a[]/
#=> premature end of char-class: /b[/
#=> /c]/
#=> warning: regular expression has ']' without escape: /[[]]/
#=> premature end of char-class: /[[]]/

If you want to use your strings as literal characters, you want to use Regexp.escape:

nilfacs.each do |fac|
p /#{Regexp.escape fac}/
end
#=> /a\[\]/
#=> /b\[/
#=> /c\]/

Alternatively, you may want to use Regexp.union to create a single regexp from your array that matches all the literal strings therein:

rejects = %w[dog cat]
re = Regexp.new(Regexp.union(rejects).source,'i') #=> /dog|cat/i
looping_finaltext = finaltext.reject{ |sentence| sentence=~re }

Double-not of an empty regex literal (!!//) is false, is this a parsing error?

This is not just applicable to empty regex literal, but rather all regex literals;

!!/(.*)/
=> false

x = /(.*)/
!!x
=> true

At first this appeared to be an issue with the way the regexp is being constructed

!!Regexp.new('')
=> true
x = Regexp.new('')
!!x
=> true
!!//.freeze
=> true

But digging deeper, this appears to be the cause:

!!//
=> false
$_ = 'b'
!!//
=> true

Setting '$_' back to nil will reset the outcome. The value for $_ is set from Kernel.readline or Kernel.get siblings. In a new console:

Kernel.readline
a
"a\n"
!!//
=> true

So instantiating a Regexp object with // appears to match it against the value of $_

Rails regex syntax error

Any - that appears inside a character class in any position other than the first or last is treated as a range, ie. [0-9] is shorthand for [0123456789]. This range is calculated based on the ASCII values.

You have \d-' in your regex and \d isn't valid to use for the start/end of a range. Probably what you want is to move - to the start or end of your []

/\A[[:alpha:]\d'’, -]\z/

...and to solve your next problem/question - as it is your regex will only match a single character, you probably also want a repeat on that character class, like a +:

/\A[[:alpha:]\d'’, -]+\z/


Related Topics



Leave a reply



Submit