# Quick Regex Query

**URL:** <https://www.ruby-forum.com/t/quick-regex-query/74450>\
**Category:** Ruby\
**Created:** [December 23, 2005, 2:19pm UTC](https://www.ruby-forum.com/t/quick-regex-query/74450 "2005-12-23T14:19:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neowulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@Neowulf](https://www.ruby-forum.com/u/Neowulf)\
**Post date:** [December 23, 2005, 2:19pm UTC](https://www.ruby-forum.com/t/quick-regex-query/74450/1 "2005-12-23T14:19:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Neowulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@Neowulf](https://www.ruby-forum.com/u/Neowulf)\
**Post date:** [December 23, 2005, 2:55pm UTC](https://www.ruby-forum.com/t/quick-regex-query/74450/2 "2005-12-23T14:55:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Neowulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@Neowulf](https://www.ruby-forum.com/u/Neowulf)\
**Post date:** [December 23, 2005, 4:04pm UTC](https://www.ruby-forum.com/t/quick-regex-query/74450/3 "2005-12-23T16:04:42Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Neowulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@Neowulf](https://www.ruby-forum.com/u/Neowulf)\
**Post date:** [December 23, 2005, 5:50pm UTC](https://www.ruby-forum.com/t/quick-regex-query/74450/4 "2005-12-23T17:50:07Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![Neowulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@Neowulf](https://www.ruby-forum.com/u/Neowulf)\
**Post date:** [December 24, 2005, 12:40am UTC](https://www.ruby-forum.com/t/quick-regex-query/74450/5 "2005-12-24T00:40:32Z")

</div>

Thanks Guys,

Top effort all round.

Regex gives me a serious head ache sometimes…

Thanks again everyone for the help.

---

<div class="post-metadata">

**Author:** ![Neowulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@Neowulf](https://www.ruby-forum.com/u/Neowulf)\
**Post date:** [December 23, 2005, 9:28pm UTC](https://www.ruby-forum.com/t/quick-regex-query/74450/6 "2005-12-23T21:28:03Z")

</div>

Robert K. 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

---

<div class="post-metadata">

**Author:** ![Neowulf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c4cdca/32.png) [@Neowulf](https://www.ruby-forum.com/u/Neowulf)\
**Post date:** [December 27, 2005, 11:58am UTC](https://www.ruby-forum.com/t/quick-regex-query/74450/7 "2005-12-27T11:58:55Z")

</div>

Florian Groß [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Robert K. 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

```
