Quick Regex Query

Discussion in 'Ruby' started by Neowulf, Dec 23, 2005.

  1. Neowulf

    Neowulf Guest

    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
     
    Neowulf, Dec 23, 2005
    #1
    1. Advertising

  2. Neowulf wrote:
    > 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.


    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?
     
    it_says_BALLS_on_your_forehead, Dec 23, 2005
    #2
    1. Advertising

  3. Neowulf wrote:
    > 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


    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
     
    Robert Klemme, Dec 23, 2005
    #3
  4. 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

    --
    http://flgr.0x42.net/
     
    Florian Groß, Dec 23, 2005
    #4
  5. Neowulf

    Neowulf Guest

    Thanks Guys,

    Top effort all round.

    Regex gives me a serious head ache sometimes...

    Thanks again everyone for the help.
     
    Neowulf, Dec 23, 2005
    #5
  6. Florian Groß <> 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
     
    Robert Klemme, Dec 27, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    11
    Views:
    936
  2. Matt Price

    quick regex question

    Matt Price, Oct 28, 2004, in forum: Python
    Replies:
    0
    Views:
    652
    Matt Price
    Oct 28, 2004
  3. Steven Bethard

    Re: quick regex question

    Steven Bethard, Oct 28, 2004, in forum: Python
    Replies:
    5
    Views:
    626
    Peter Otten
    Oct 29, 2004
  4. Robert Brewer

    RE: quick regex question

    Robert Brewer, Oct 28, 2004, in forum: Python
    Replies:
    0
    Views:
    647
    Robert Brewer
    Oct 28, 2004
  5. Replies:
    3
    Views:
    823
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page