Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to match last word?

Reply
Thread Tools

How to match last word?

 
 
camenix
Guest
Posts: n/a
 
      11-05-2006
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)

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      11-05-2006
camenix <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
camenix
Guest
Posts: n/a
 
      11-05-2006


On Nov 5, 8:52 pm, "Robert Klemme" <(E-Mail Removed)> wrote:
> camenix <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
re.sub(): replace longest match instead of leftmost match? John Gordon Python 13 12-20-2011 02:58 AM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments