[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

regular expressions

Tom Allison

10/3/2006 12:01:00 AM

I'm sorry if this has been done before, but I can't seem to find any good examples:

I'm trying to search mail messages for headers based on the following:

config = YAML.load_file('config.yaml')

$stdin.each do |line|
puts line if line =~ /^Recevied:/ .. line =~ /by #{config['hostname']}/o
end

and guess what? It doesn't work.
If I hard code the hostname it works fine.
But this and /by config['hostname']/ both fail to match.

So, how do you set a variable in a regular expression?

Also, how would I match multiple lines?

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.

9 Answers

MonkeeSage

10/3/2006 12:27:00 AM

0

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

Paul Lutus

10/3/2006 12:37:00 AM

0

Tom Allison wrote:

> I'm sorry if this has been done before, but I can't seem to find any good
> examples:
>
> I'm trying to search mail messages for headers based on the following:
>
> config = YAML.load_file('config.yaml')
>
> $stdin.each do |line|
> puts line if line =~ /^Recevied:/ .. line =~ /by
> #{config['hostname']}/o
> end
>
> and guess what? It doesn't work.

Yes, it doesn't, and I think I know why. I have been hearing for years how
it just doesn't matter whether young people learn how to spell common
words, and I have steadfastly taken the position that it does matter.

It turns out that people who can't spell, and who leave school confident
that it is an out-of-date art, slowly discover all the odd places where
knowing how to spell actually makes a difference.

So, now that you are living in reality, perhaps you will discover one of the
problems with your regular expression is that "Recevied" isn't a word, and
it won't filter out those e-mails you want to collect.

--
Paul Lutus
http://www.ara...

Tom Allison

10/3/2006 12:39:00 AM

0

MonkeeSage wrote:
> 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
>

This prints out the entire message.

I'm only trying to get the one section that matched the Received header.
I should be able to do with with the statement
puts line if line =~ /Received/ .. line =~ /#{config['hostname']}/
At least that's what I'm believe I'm being led towards.

But the match on /Received/ turns on the printing and it never matches on the
second regexp.

Paul Lutus

10/3/2006 1:09:00 AM

0

Tom Allison wrote:

> MonkeeSage wrote:
>> 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
>>
>
> This prints out the entire message.

Well, if the regexp as written correctly identifies the beginning and end of
the desired area, how about this:

result = line.sub(/^(Received:.*?by #{config['hostname']}).*/m,"\\1")

puts result if result.size > 0

Note the question mark in the middle of the regexp. It means to stop at the
first match, not the last, of what follows. This change may not matter in
practice, but it is a good habit to fall into when dealing with a lot of
data. One special case might fail after thousands of successful matches.

This hasn't been tested.

--
Paul Lutus
http://www.ara...

MonkeeSage

10/3/2006 1:19:00 AM

0

Tom Allison wrote:
> I'm only trying to get the one section that matched the Received header.
> I should be able to do with with the statement
> puts line if line =~ /Received/ .. line =~ /#{config['hostname']}/
> At least that's what I'm believe I'm being led towards.

In ruby .. is a range operator. If you want a grouped match, use parens
and a backreference just like perl.

$stdin.each { |line|
puts $1 if line =~ /^Received:(.+?)\s{4,}by #{config['hostname']}/
# prints " from mail.papa.smurf (mail.papa.smurf [127.0.0.1])"
}

Regards,
Jordan

Eero Saynatkari

10/3/2006 2:24:00 AM

0

On 2006.10.03 10:20, MonkeeSage wrote:
> Tom Allison wrote:
> > I'm only trying to get the one section that matched the Received header.
> > I should be able to do with with the statement
> > puts line if line =~ /Received/ .. line =~ /#{config['hostname']}/
> > At least that's what I'm believe I'm being led towards.
>
> In ruby .. is a range operator. If you want a grouped match, use parens
> and a backreference just like perl.
>
> $stdin.each { |line|
> puts $1 if line =~ /^Received:(.+?)\s{4,}by #{config['hostname']}/
> # prints " from mail.papa.smurf (mail.papa.smurf [127.0.0.1])"
> }

And you almost certainly want to Regexp.escape that interpolation.

Tom Allison

10/3/2006 9:41:00 AM

0

Paul Lutus wrote:
> Tom Allison wrote:
>
>> I'm sorry if this has been done before, but I can't seem to find any good
>> examples:
>>
>> I'm trying to search mail messages for headers based on the following:
>>
>> config = YAML.load_file('config.yaml')
>>
>> $stdin.each do |line|
>> puts line if line =~ /^Recevied:/ .. line =~ /by
>> #{config['hostname']}/o
>> end
>>
>> and guess what? It doesn't work.
>
> Yes, it doesn't, and I think I know why. I have been hearing for years how
> it just doesn't matter whether young people learn how to spell common
> words, and I have steadfastly taken the position that it does matter.
>

You make a pretty good example of another lost art...

Leslie Viljoen

10/3/2006 1:38:00 PM

0

On 10/3/06, Tom Allison <tallison@tacocat.net> wrote:
> I'm sorry if this has been done before, but I can't seem to find any good examples:
>
> I'm trying to search mail messages for headers based on the following:
>
> config = YAML.load_file('config.yaml')
>
> $stdin.each do |line|
> puts line if line =~ /^Recevied:/ .. line =~ /by #{config['hostname']}/o
> end
>
> and guess what? It doesn't work.
> If I hard code the hostname it works fine.
> But this and /by config['hostname']/ both fail to match.

To eliminate a possibility, do

p config['hostname']

just before the $stdin line there. I can't tell you how many times I
have forgotten about invisible newlines tacked onto my variables that
cause all sorts of matching to fail.


Les

Gerard

8/21/2011 7:53:00 PM

0

M forever wrote:
> On Aug 21, 3:20 pm, "Gerard" <ghe_no_spam_ndrik...@hotmail.com> wrote:
> > M forever wrote:
> > > On Aug 21, 3:10 pm, "Gerard" <ghe_no_spam_ndrik...@hotmail.com>
> > > wrote:
> > > > M forever wrote:
> > > > > On Aug 21, 2:12 pm, "Gerard"
> > > > > <ghe_no_spam_ndrik...@hotmail.com> wrote:
> > > > > > M forever wrote:
> > > > > > > On Aug 21, 2:02 pm, "Gerard"
> > > > > > > <ghe_no_spam_ndrik...@hotmail.com> wrote:
> > > > > > > > M forever wrote:
> > > > > > > > > On Aug 21, 1:40 pm, "Gerard"
> > > > > > > > > <ghe_no_spam_ndrik...@hotmail.com> wrote:
> > > > > > > > > > M forever wrote:
> > > > > > > > > > > On Aug 21, 9:58 am, Bob Harper
> > > > > > > > > > > <bob.har...@comcast.net> wrote:
> > > > > > > > > > > > On 8/20/11 11:59 PM, M forever wrote:
> > > > > > > > > > > > (snip)> It actually makes me proud to be the
> > > > > > > > > > > > object of your fanatic hatred.
> >
> > > > > > > > > > > > (snip)
> >
> > > > > > > > > > > > For the record, I *do not* 'hate' you. I dislike
> > > > > > > > > > > > your behavior here, that's all. Again, I do not
> > > > > > > > > > > > hate you, I pity you. There's a difference;
> > > > > > > > > > > > I hope someday you'll figure that out and change
> > > > > > > > > > > > your behavior. When you do, the need for that
> > > > > > > > > > > > pity will be obviated.
> >
> > > > > > > > > > > So why should I take advice from a guy who
> > > > > > > > > > > condones the rape of children and who thinks it's
> > > > > > > > > > > OK the perpetrators are not criminally prosecuted?
> >
> > > > > > > > > > > Why should I take advice from a guy who appoints
> > > > > > > > > > > himself the moral apostle but who himself has
> > > > > > > > > > > absolutely no manners and no respect when it
> > > > > > > > > > > comes to participation in discussions? A guy who,
> > > > > > > > > > > when his views are challenged, can not make an
> > > > > > > > > > > argument for his views, all he can do is to
> > > > > > > > > > > snip&snipe and defame?
> >
> > > > > > > > > > > In other words - you.
> >
> > > > > > > > > > > Even this "well meant advice" is completely
> > > > > > > > > > > hypocritical. You just can't live with the fact
> > > > > > > > > > > that I have challenged you in a number of
> > > > > > > > > > > discussions and you weren't able to make your own
> > > > > > > > > > > points and all you had was that childish behavior
> > > > > > > > > > > described above. So, you hold a strong personal
> > > > > > > > > > > grudge against me just like a mentally immature
> > > > > > > > > > > person like you would, and you try to hide that
> > > > > > > > > > > behind your sanctimoniousness.
> >
> > > > > > > > > > There it is again.
> > > > > > > > > > And it goes on and on and on and on and on and on
> > > > > > > > > > and on and on and on and on and on and on and on
> > > > > > > > > > and on and on and on and on. The FM forever.
> >
> > > > > > > > > Man, you really *are* online *night and day*, *seven
> > > > > > > > > days a week*. It only took you a few minutes to
> > > > > > > > > "respond".
> >
> > > > > > > > > So I have a question for you, too. *Why* do you spend
> > > > > > > > > all your time hovering around this forum, commenting
> > > > > > > > > on everything, while nobody here takes you seriously
> > > > > > > > > and few people ever even reply to you.
> >
> > > > > > > > There it is again.
> > > > > > > > And it goes on and on and on and on and on and on and on
> > > > > > > > and on and on and on and on and on and on and on and on
> > > > > > > > and on and on. The FM forever.
> >
> > > > > > > > > Why do you "comment" on everything I say? Why do you
> > > > > > > > > always have to make a contrarian comment? Like when I
> > > > > > > > > said, the economy is very regulated in Germany, you
> > > > > > > > > reply "Deutschland ?ber alles". But the same applies
> > > > > > > > > to the Netherlands where you sit in front of your
> > > > > > > > > computer night and day. They also have a stable
> > > > > > > > > economy because it is regulated, pretty much in the
> > > > > > > > > same tried and tested ways as in Germany. So why is
> > > > > > > > > that suddenly something bad?
> >
> > > > > > > > As you know very well, it was *completely* irrelevant.
> > > > > > > > It had *nothing* to do with the "discussion" at hand.
> > > > > > > > Just chauvinistic blathering as usual.
> >
> > > > > > > Saying the economy Germany is working better *because it
> > > > > > > is regulated* in response to a discussion in which
> > > > > > > somebody stated that "regulations kill the economy" is
> > > > > > > very relevant and has a lot to do with the
> >
> > > > > > BS - see below.
> >
> > > > > > > discussion at hand. It gives a good example for why
> > > > > > > regulations do not kill, but in fact protect the economy.
> >
> > > > > > > On the other hand, it is not "chauvinist" at all.
> > > > > > > Especially since the same statement applies to other
> > > > > > > countries as well, for instance, yours. You didn't think
> > > > > > > of that before you typed, did you?
> >
> > > > > > Even more irrelevant.
> > > > > > Pure BS. Nonsense.
> >
> > > > > No, it makes complete sense, and it just explained why.
> >
> > > > No, you did not explain anything.
> > > > What you did, was cutting in my post (and in what you quoted):
> >
> > > > Socialist Reverend Jim Jones took his San Fran flock to his new
> > > > "Utopia" in Jonestown, Guyana. There, he became a mini Dictator,
> > > > mistreating his people and molesting children. When a California
> > > > Congressman (a Democrat, no less) went to investigate, Jones
> > > > had him murdered along with some of the people who tried to
> > > > escape with him. He then ordered his members to drink poisoned
> > > > Kool aid and they all died.
> >
> > > > That is "Drinking the Kool Aid" --believing in Marxism.
> > > > <<<<<<<<<<<<<<<<<
> >
> > > > > It's not
> > > > > really that difficult to understand nor that controversial a
> > > > > statement. Especially since it applies to your country as
> > > > > well. Nothing "irrelevant" or "chauvinist" there at all.
> >
> > > > The broken record FM forever, and it goes on and on and on and
> > > > on and on and on and on and on and on and on and on and on and
> > > > on and on and on and on and on and on and on and on and on.
> >
> > > > > But you didn't think that far before you typed in your
> > > > > automated "response", obviously. With the number of posts you
> > > > > make every day, there is not really time to think about what
> > > > > you write. Especially since you "reply" the same stuff to
> > > > > every one of my posts, no matter what it is actually about.
> >
> > > > > So, back to my question: why is that? Why are you so obsessed
> > > > > with replying to everything on this forum here, night and
> > > > > day, every day of the week? Why are you so obsessed with
> > > > > "commenting" on everything I post here?
> >
> > > > And there it is again, the FM forever, and it goes on and on
> > > > and on and on and on and on and on and on and on and on and on
> > > > and on and on and on and on and on and on and on and on and on
> > > > and on and on and on and on and on and on and on and on and on
> > > > and on and on and on and on and on and on and on and on and on
> > > > and on and on and on and on and on and on and on and on and on
> > > > and on and on and on.
> >
> > > You didn't answer any of my questions. You just avoided them and
> > > then posted your usual OCD/ODD stuff "and on and on and on" -
> > > impressive how many times you repeated that. This very obviously
> > > is an obsession for you.
> >
> > > But *why* is that? If you don't like my posts, but you don't have
> > > anything to reply either other than just the same standard
> > > phrases, why do you keep obsessing about them? Why don't you just
> > > ignore them the way most people here ignore you and the way I
> > > ignore you most of the time? Yet you keep posting the same
> > > contrarian "comments" over and over and over, or "on and on and
> > > on" and "forever".
> > > So why is that?
> >
> > See?
> > There it is again, the FM forever, and it never stops and always
> > writes the same things again and again.
> > It goes on and on and on and on and on and on and on and on and on
> > and on and on and on and on and on and on and on and on and on and
> > on and on and on and on and on and on and on and on and on and on
> > and on and on and on and on and on and on and on and on and on and
> > on and on and on and on and on and on and on and on and on and on
> > and on and on and on.
>
> Still no answer to any of my questions. So I think it is pretty safe

See?
It never stops, a FM forever.


> to say that there is no point in even trying to take you seriously.

HAHAHAHA!
What a silly remark.
You have said so hundreds of times already.

>
> Now, that has been proven many times before, but I thought I just give
> you the benefit of the doubt

HAHAHAHAHAHAHA!
What a stupid remark.
You have said so hundreds of times already.

>
> and inquire why you keep repeating the
> same obsessive stuff in "response" to all my posts.
> But it is obvious that there is no reason behind that, and you simply
> can't help it.
> When I am offline, I can see you just switch to other posters, so it
> doesn't really have anything to do with me either.

And it goes on and on and on and on and on and on and on ....
It's a silly FM forever.