0

I have been trying to filter some text from a file to only show certain years of birth

I have some ruby that reads in a file ages.txt, it contains names and years of birth

  • Joe Bloggs (2001)
  • Mary Bloggs (1987)
  • John apples (2010)
  • Old Guy (1990)

I wish to be able to filter for a year range, i.e. to find people born between 1987 and 1990

I have tried

#open('ages.txt') { |f| f.grep(/^19(8[0-9]|10)$/) }

but I get an error

ages.rb:3:in `===': invalid byte sequence in UTF-8 (ArgumentError)

Any help would be greatfull.

LostInTranslation
  • 159
  • 1
  • 1
  • 10
Nemothefish
  • 47
  • 10

1 Answers1

1

The ^ and the $ make the expression try to match whole lines, which will not work.

Try: /19(8[7-9]|90)/

Here is a working version. Let me know if this helps.

Anthony Clark
  • 563
  • 5
  • 17