How to match last word?

Discussion in 'Ruby' started by camenix, Nov 5, 2006.

  1. camenix

    camenix Guest

    Hi,all
    I has a script to match 'Transfer dbf to SQLServer', the last
    comand successfully ended.But I found no way to match last
    command.Whoever can help me?

    main.rb
    string='
    Program begin at 2006-11-05T15:01:13+0800
    Change image name to lower begin at 2006-11-05T15:01:13+0800
    Change image name to lower end at 2006-11-05T15:01:14+0800
    Transfer dbf to SQLServer begin at 2006-11-05T15:01:14+0800
    Transfer dbf to SQLServer end at 2006-11-05T15:01:15+0800
    Modify Tables begin at 2006-11-05T15:01:15+0800
    '
    re=/\n{0,1}(.+?)\s+(end\s+at)(.+?)\n/i
    re.match(string)
    puts $1,$2,$3,$4

    The string is came from a file,so the "\n" maybe ommited,but
    /\n{0,1}(.+?)\s+(end\s+at)(.+?)(\n|\Z)/i didn't work.
    This form of regular expresion always match 'Change image name to
    lower'.($2)
     
    camenix, Nov 5, 2006
    #1
    1. Advertising

  2. camenix <> wrote:
    > Hi,all
    > I has a script to match 'Transfer dbf to SQLServer', the last
    > comand successfully ended.But I found no way to match last
    > command.Whoever can help me?
    >
    > main.rb
    > string='
    > Program begin at 2006-11-05T15:01:13+0800
    > Change image name to lower begin at 2006-11-05T15:01:13+0800
    > Change image name to lower end at 2006-11-05T15:01:14+0800
    > Transfer dbf to SQLServer begin at 2006-11-05T15:01:14+0800
    > Transfer dbf to SQLServer end at 2006-11-05T15:01:15+0800
    > Modify Tables begin at 2006-11-05T15:01:15+0800
    > '
    > re=/\n{0,1}(.+?)\s+(end\s+at)(.+?)\n/i
    > re.match(string)
    > puts $1,$2,$3,$4
    >
    > The string is came from a file,so the "\n" maybe ommited,but
    > /\n{0,1}(.+?)\s+(end\s+at)(.+?)(\n|\Z)/i didn't work.
    > This form of regular expresion always match 'Change image name to
    > lower'.($2)


    >> string='

    Program begin at 2006-11-05T15:01:13+0800
    Change image name to lower begin at 2006-11-05T15:01:13+0800
    Change image name to lower end at 2006-11-05T15:01:14+0800
    Transfer dbf to SQLServer begin at 2006-11-05T15:01:14+0800
    Transfer dbf to SQLServer end at 2006-11-05T15:01:15+0800
    Modify Tables begin at 2006-11-05T15:01:15+0800
    '
    >> puts string.scan(%r{^(.*)\s+(?:begin|end) at .*$})

    Program
    Change image name to lower
    Change image name to lower
    Transfer dbf to SQLServer
    Transfer dbf to SQLServer
    Modify Tables
    => nil

    Not the most efficient RX but seems to work - at least according to what I
    gather from your posting.

    Kind regards

    robert
     
    Robert Klemme, Nov 5, 2006
    #2
    1. Advertising

  3. camenix

    camenix Guest

    On Nov 5, 8:52 pm, "Robert Klemme" <> wrote:
    > camenix <> wrote:
    > > Hi,all
    > > I has a script to match 'Transfer dbf to SQLServer', the last
    > > comand successfully ended.But I found no way to match last
    > > command.Whoever can help me?

    >
    > > main.rb
    > > string='
    > > Program begin at 2006-11-05T15:01:13+0800
    > > Change image name to lower begin at 2006-11-05T15:01:13+0800
    > > Change image name to lower end at 2006-11-05T15:01:14+0800
    > > Transfer dbf to SQLServer begin at 2006-11-05T15:01:14+0800

    ap> > Transfer dbf to SQLServer end at 2006-11-05T15:01:15+0800
    > > Modify Tables begin at 2006-11-05T15:01:15+0800
    > > '
    > > re=/\n{0,1}(.+?)\s+(end\s+at)(.+?)\n/i
    > > re.match(string)
    > > puts $1,$2,$3,$4

    >
    > > The string is came from a file,so the "\n" maybe ommited,but
    > > /\n{0,1}(.+?)\s+(end\s+at)(.+?)(\n|\Z)/i didn't work.
    > > This form of regular expresion always match 'Change image name to
    > > lower'.($2)
    > >> string='Program begin at 2006-11-05T15:01:13+0800

    > Change image name to lower begin at 2006-11-05T15:01:13+0800
    > Change image name to lower end at 2006-11-05T15:01:14+0800
    > Transfer dbf to SQLServer begin at 2006-11-05T15:01:14+0800
    > Transfer dbf to SQLServer end at 2006-11-05T15:01:15+0800
    > Modify Tables begin at 2006-11-05T15:01:15+0800
    > '>> puts string.scan(%r{^(.*)\s+(?:begin|end) at .*$})Program
    > Change image name to lower
    > Change image name to lower
    > Transfer dbf to SQLServer
    > Transfer dbf to SQLServer
    > Modify Tables
    > => nil
    >
    > Not the most efficient RX but seems to work - at least according to what I
    > gather from your posting.
    >
    > Kind regards
    >
    > robert

    It worked!
    re=/^(.+?)\s+(end\s+at)(.+?)$/i
    puts s.match(re)
    result:Change image name to lower end at 2006-11-05T15:01:14+0800
    $2==Change image name to lower
    puts s.scan(re)
    result:
    Change image name to lower
    end at
    2006-11-05T15:01:14+0800
    Transfer dbf to SQLServer
    end at
    2006-11-05T15:01:15+0800
    $2==Transfer dbf to SQLServer

    appreciate your help.
     
    camenix, Nov 5, 2006
    #3
    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. hiwa
    Replies:
    0
    Views:
    640
  2. Victor
    Replies:
    2
    Views:
    649
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    374
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    490
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    168
Loading...

Share This Page