Forum: Ruby Quick Regex Query

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D8376641b56fed67d99599d5e7bf9631?d=identicon&s=25 Neowulf (Guest)
on 2005-12-23 14:19
(Received via mailing list)
Hi all,

Really quick one for the regex gurus...

Anyone know of a regex string which will extract out the stuff between
"from" and "not" without the spaces between?

User root from toronto-hs-216-138-233-211.s-ip.magma.ca not allowed
because not listed in AllowUsers

I've found something which *almost* does the job...

\bfrom\W+(?:\w+\W+){1,6}not\b

This is limited however, as I can't know for certain how many "words"
will make up the hostname.

I've got a non-regex solution already, but you don't want to know how
that one works...

.... something to do with using split and relying on the fact that the
host name field always end up at the same index.

Seems more than a little clunky.

Any advise appreciated.

Cheers

~Neowulf
694b93d6e852a72e6afe58d3ef4daf66?d=identicon&s=25 it_says_BALLS_on_your_forehead (Guest)
on 2005-12-23 14:55
(Received via mailing list)
Neowulf wrote:
> I've found something which *almost* does the job...
>
> \bfrom\W+(?:\w+\W+){1,6}not\b
>
> This is limited however, as I can't know for certain how many "words"
> will make up the hostname.

why do the number of words matter? i thought you only needed the
'stuff' between 'from' and 'not'. now when you say 'without the spaces
between', do you mean the spaces immediately after the from, and before
the not? what's wrong with:

/from\s+(\w+)\s+not/ && print "$1"   # i'm more familiar with Perl, not
sure if $1 works in
                                                    # ruby, but it
should contain what's in capturing parens

can you provide several examples of input and expected output that
would illustrate your requirements?
37a3c73ffbf864e4b28f7f2384ee12ce?d=identicon&s=25 Timothy Hunter (tim-hunter)
on 2005-12-23 16:04
(Received via mailing list)
Neowulf wrote:
> I've found something which *almost* does the job...
> host name field always end up at the same index.
>
> Seems more than a little clunky.
>
> Any advise appreciated.
>
> Cheers
>
> ~Neowulf
>

str = "User root from toronto-hs-216-138-233-211.s-ip.magma.ca not
allowed because not listed in AllowUsers"

m = /from (.*?) not/.match(str)
puts m[1]
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2005-12-23 17:50
(Received via mailing list)
Neowulf wrote:
> I've found something which *almost* does the job...
>
> \bfrom\W+(?:\w+\W+){1,6}not\b

host_name = /from\s+(\S+)\s+not/ =~ s && $1

> This is limited however, as I can't know for certain how many "words"
> will make up the hostname.

But there won't be any whitespace in the hostname, right?  Then the RX
above will do it.

Kind regards

    robert
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 =?ISO-8859-1?Q?Florian_Gro=DF?= (Guest)
on 2005-12-23 21:28
(Received via mailing list)
Robert Klemme wrote:

> host_name = /from\s+(\S+)\s+not/ =~ s && $1

Tsk, those Perl guys... ;)

host_name[/from\s+(\S+)\s+not/, 1] # Will either return nil or a String
D8376641b56fed67d99599d5e7bf9631?d=identicon&s=25 Neowulf (Guest)
on 2005-12-24 00:40
(Received via mailing list)
Thanks Guys,

Top effort all round.

Regex gives me a serious head ache sometimes...

Thanks again everyone for the help.
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2005-12-27 11:58
(Received via mailing list)
Florian GroƟ <florgro@gmail.com> wrote:
> Robert Klemme wrote:
>
>> host_name = /from\s+(\S+)\s+not/ =~ s && $1
>
> Tsk, those Perl guys... ;)

Ssshhh!  I'm trying to get over my Perl heritage but sometimes it still
haunts me... :-)

> host_name[/from\s+(\S+)\s+not/, 1] # Will either return nil or a
> String

Thanks for reminding me - I always keep forgetting this variant,
especially
with the group selector!

Kind regards

    robert
This topic is locked and can not be replied to.