From: MonkeeSage Date: 2006-10-03T09:30:11+09:00 Subject: Re: regular expressions Tom Allison wrote: > So, how do you set a variable in a regular expression? You have it right. > Also, how would I match multiple lines? You're right again with the //m. But you can't read line-by-line (IO#each) and match across multiple lines without building some kind of parser. It's usually better to just read the data into a single string for multiline matches. Input (multiline): Received: from mail.papa.smurf (mail.papa.smurf [127.0.0.1]) by down@fraggle.roc config = {'hostname' => 'down@fraggle.roc'} line = $stdin.read puts line if line =~ /^Received:.*by #{config['hostname']}/m > I would much rather match something like: > > /^Received:.+?\s{4,}by #{config['hostname']}/sm > because that will pick up the one Received header I want. > I can write in Perl 5 regex but I'm not as familiar with Ruby's methods. Input (single line): Received: from mail.papa.smurf (mail.papa.smurf [127.0.0.1]) by down@fraggle.roc $stdin.each { |line| puts line if line =~ /^Received:.+?\s{4,}by #{config['hostname']}/ } Regards, Jordan