[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question about regular expression

Eric Luo

1/17/2006 2:35:00 AM

I need to hack out an regular expression, which will match "SNPB" without
matching "STR SNPB".
Since ruby 1.8.2 or 1.8.4 don't support the lookbehind feature. what's the
workable regular expression
for ruby version 1.8.2?

Thanks for any idea.

Eric
12 Answers

James Gray

1/17/2006 2:42:00 AM

0

On Jan 16, 2006, at 8:35 PM, Eric Luo wrote:

> I need to hack out an regular expression, which will match "SNPB"
> without
> matching "STR SNPB".
> Since ruby 1.8.2 or 1.8.4 don't support the lookbehind feature.
> what's the
> workable regular expression
> for ruby version 1.8.2?

Lookbehind is just lookahead, backwards. ;)

>> tests = %w{SNPB STR\ SNPB}
=> ["SNPB", "STR SNPB"]
>> tests.map { |t| t.reverse }.grep(/\bBPNS\b(?! RTS)/).map { |t|
t.reverse }
=> ["SNPB"]

Hope that helps.

James Edward Gray II


Eric Luo

1/17/2006 3:20:00 AM

0

Thanks for your replay.

To make the problem clear.
I want to only change the regular expression, but not the code to implement
this function.
Actually, the regular expression comes from a configurable table. I'll be
supposed to only have the privilege to update the table.

So What I really want is a alternative way to in place of the regular
expression
(?<!STR )SNPB


2006/1/17, James Edward Gray II <james@grayproductions.net>:
>
> On Jan 16, 2006, at 8:35 PM, Eric Luo wrote:
>
> > I need to hack out an regular expression, which will match "SNPB"
> > without
> > matching "STR SNPB".
> > Since ruby 1.8.2 or 1.8.4 don't support the lookbehind feature.
> > what's the
> > workable regular expression
> > for ruby version 1.8.2?
>
> Lookbehind is just lookahead, backwards. ;)
>
> >> tests = %w{SNPB STR\ SNPB}
> => ["SNPB", "STR SNPB"]
> >> tests.map { |t| t.reverse }.grep(/\bBPNS\b(?! RTS)/).map { |t|
> t.reverse }
> => ["SNPB"]
>
> Hope that helps.
>
> James Edward Gray II
>
>

Ara.T.Howard

1/17/2006 4:34:00 AM

0

Simon Strandgaard

1/17/2006 5:00:00 AM

0

On 1/17/06, Eric Luo <eric.wenbl@gmail.com> wrote:
> I need to hack out an regular expression, which will match "SNPB" without
> matching "STR SNPB".
> Since ruby 1.8.2 or 1.8.4 don't support the lookbehind feature. what's the
> workable regular expression
> for ruby version 1.8.2?

tests = %w{SNPB STR\ SNPB}
re = /(?<!STR )SNPB/
tests.map{|t| t.match(re) }

#=> [#<MatchData:0x65704>, nil]


ruby 1.8.2 (2004-11-03) [powerpc-darwin7.5.0]


--
Simon Strandgaard


Joel VanderWerf

1/17/2006 5:05:00 AM

0

Simon Strandgaard wrote:
> On 1/17/06, Eric Luo <eric.wenbl@gmail.com> wrote:
>> I need to hack out an regular expression, which will match "SNPB" without
>> matching "STR SNPB".
>> Since ruby 1.8.2 or 1.8.4 don't support the lookbehind feature. what's the
>> workable regular expression
>> for ruby version 1.8.2?
>
> tests = %w{SNPB STR\ SNPB}
> re = /(?<!STR )SNPB/
> tests.map{|t| t.match(re) }
>
> #=> [#<MatchData:0x65704>, nil]
>
>
> ruby 1.8.2 (2004-11-03) [powerpc-darwin7.5.0]
>
>
> --
> Simon Strandgaard
>

Simon, do you have oniguruma installed? This is what I get:

$ ruby -v -e '/(?<!STR )SNPB/'
ruby 1.8.4 (2005-12-24) [i686-linux]
-e:1: undefined (?...) sequence: /(?<!STR )SNPB/
-e:1: warning: useless use of a literal in void context

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Simon Strandgaard

1/17/2006 5:15:00 AM

0

On 1/17/06, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> Simon Strandgaard wrote:
[snip]
> > #=> [#<MatchData:0x65704>, nil]
> >
> > ruby 1.8.2 (2004-11-03) [powerpc-darwin7.5.0]
> >
>
> Simon, do you have oniguruma installed? This is what I get:
[snip]

hmm.. it seem so. Sorry.

--
Simon Strandgaard


Eric Luo

1/17/2006 7:17:00 AM

0

Thanks very much, I really appreatiate your help

It does work, but I couldn't figure out what the ^ and * do? could you
explain that in more detail?

Thanks


2006/1/17, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov>:
>
> On Tue, 17 Jan 2006, Eric Luo wrote:
>
> > Thanks for your replay.
> >
> > To make the problem clear.
> > I want to only change the regular expression, but not the code to
> implement
> > this function.
> > Actually, the regular expression comes from a configurable table. I'll
> be
> > supposed to only have the privilege to update the table.
> >
> > So What I really want is a alternative way to in place of the regular
> > expression
> > (?<!STR )SNPB
>
> harp:~ > cat a.rb
> strings = "STR SNPB", "STR", "SNPB"
>
> re = %r/^ (?: (?:[^S]) | (?:S[^T]) | (?:ST[^R]) )* SNPB /ox
>
> strings.each do |string|
> permutations = string, "foo #{ string }", "#{ string } bar", "foo #{
> string } bar"
> permutations.each do |permutation|
> puts "<#{ permutation }> matches" if re.match permutation
> end
> end
>
> harp:~ > ruby a.rb
> <SNPB> matches
> <foo SNPB> matches
> <SNPB bar> matches
> <foo SNPB bar> matches
>
> hth.
>
> -a
> --
> strong and healthy, who thinks of sickness until it strikes like
> lightning?
> preoccupied with the world, who thinks of death, until it arrives like
> thunder? -- milarepa
>
>
>

Eric Luo

1/19/2006 6:26:00 AM

0

In your solution:

<SNPB> <foo SNPB> <SNPB bar> <foo SNPB bar> matched.

But "STR bla SNPB " will also be matched, which is not expected.

I really appreciate your help. Thanks

2006/1/17, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov>:
>
> On Tue, 17 Jan 2006, Eric Luo wrote:
>
> > Thanks for your replay.
> >
> > To make the problem clear.
> > I want to only change the regular expression, but not the code to
> implement
> > this function.
> > Actually, the regular expression comes from a configurable table. I'll
> be
> > supposed to only have the privilege to update the table.
> >
> > So What I really want is a alternative way to in place of the regular
> > expression
> > (?<!STR )SNPB
>
> harp:~ > cat a.rb
> strings = "STR SNPB", "STR", "SNPB"
>
> re = %r/^ (?: (?:[^S]) | (?:S[^T]) | (?:ST[^R]) )* SNPB /ox
>
> strings.each do |string|
> permutations = string, "foo #{ string }", "#{ string } bar", "foo #{
> string } bar"
> permutations.each do |permutation|
> puts "<#{ permutation }> matches" if re.match permutation
> end
> end
>
> harp:~ > ruby a.rb
> <SNPB> matches
> <foo SNPB> matches
> <SNPB bar> matches
> <foo SNPB bar> matches
>
> hth.
>
> -a
> --
> strong and healthy, who thinks of sickness until it strikes like
> lightning?
> preoccupied with the world, who thinks of death, until it arrives like
> thunder? -- milarepa
>
>
>

Andrew Johnson

1/19/2006 7:10:00 AM

0

On Thu, 19 Jan 2006 15:25:50 +0900, Eric Luo <eric.wenbl@gmail.com> wrote:

> In your solution:
>
> <SNPB> <foo SNPB> <SNPB bar> <foo SNPB bar> matched.
>
> But "STR bla SNPB " will also be matched, which is not expected.

So your specification is to match SNPB only when no STR occurs anywhere
prior to the target? Then you'll have to creep along the string from the
beginning:

re = %r/^((?!STR).)*SNPB/
strs = ["blah SNPB good", "blah STR SNPB bad", "STRING SNPB bad"]
strs.each {|s| puts s if s.match re}

regards,
andrew

Stefan Walk

1/19/2006 9:35:00 AM

0

On 19/01/06, Eric Luo <eric.wenbl@gmail.com> wrote:
> In your solution:
>
> <SNPB> <foo SNPB> <SNPB bar> <foo SNPB bar> matched.
>
> But "STR bla SNPB " will also be matched, which is not expected.
>
> I really appreciate your help. Thanks
>

Then the lookbehind assertion would not have worked either. (?<!A)B
says "Match any B that is not *immediately* preceded by an A.

Regards,
Stefan