Question about regular expression

E

Eric Luo

------=_Part_6277_16506322.1137465300265
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

------=_Part_6277_16506322.1137465300265--
 
J

James Edward Gray II

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
 
E

Eric Luo

------=_Part_6461_30171838.1137467977108
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 said:
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 =3D %w{SNPB STR\ SNPB} =3D> ["SNPB", "STR SNPB"]
tests.map { |t| t.reverse }.grep(/\bBPNS\b(?! RTS)/).map { |t|
t.reverse }
=3D> ["SNPB"]

Hope that helps.

James Edward Gray II

------=_Part_6461_30171838.1137467977108--
 
A

ara.t.howard

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
 
S

Simon Strandgaard

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 t= he
workable regular expression
for ruby version 1.8.2?

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

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


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

Joel VanderWerf

Simon said:
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, 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
 
E

Eric Luo

------=_Part_7521_27313026.1137482234873
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 said:
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 =3D "STR SNPB", "STR", "SNPB"

re =3D %r/^ (?: (?:[^S]) | (?:S[^T]) | (?:ST[^R]) )* SNPB /ox

strings.each do |string|
permutations =3D 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

------=_Part_7521_27313026.1137482234873--
 
E

Eric Luo

------=_Part_2242_30453736.1137651937457
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 said:
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 =3D "STR SNPB", "STR", "SNPB"

re =3D %r/^ (?: (?:[^S]) | (?:S[^T]) | (?:ST[^R]) )* SNPB /ox

strings.each do |string|
permutations =3D 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

------=_Part_2242_30453736.1137651937457--
 
A

Andrew Johnson

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
 
S

Stefan Walk

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
 
A

ara.t.howard

In your solution:

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

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

it should not:

irb(main):002:0> %r/^ (?: (?:[^S]) | (?:S[^T]) | (?:ST[^R]) )* SNPB /ox.match "STR bla SNPB "
=> nil

and does not on any of my machines. are you seeing something different?



the way that regular expression reads is:

- beginning of line

- zero or more things that are either
- not an S
- an S followed by not a T
- or ST followed by not an R

- the string SNPB

(we ignore the remainder of the string, though you could do more here if
needed)

so the appearance of the string STR __anywhere__ before a SNPB will cause the
match to fail.

hth.

-a
 
E

Eric Luo

------=_Part_217_18789833.1137722964119
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Sorry for my mistake.

I do want to let "STR bla SNPB" matched. but I said it mistakenly in the
opposite in my lastest post.

I want that the appearance of the word rather string STR just before a SNPB
will cause the match to fail.

That is, "STR bla SNPB", "PreSTR SNPB" "STRSNPB" will be matched.
but "STR SNPB" will not.




2006/1/20 said:
In your solution:

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

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

it should not:

irb(main):002:0> %r/^ (?: (?:[^S]) | (?:S[^T]) | (?:ST[^R]) )* SNPB
/ox.match "STR bla SNPB "
=3D> nil

and does not on any of my machines. are you seeing something different?



the way that regular expression reads is:

- beginning of line

- zero or more things that are either
- not an S
- an S followed by not a T
- or ST followed by not an R

- the string SNPB

(we ignore the remainder of the string, though you could do more here
if
needed)

so the appearance of the string STR __anywhere__ before a SNPB will cause
the
match to fail.

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

------=_Part_217_18789833.1137722964119--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top