[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

RegEx help (small

Bil Kleb

1/27/2005 9:59:00 AM

I'm too dense to figure this one out today: The
second (and probably third) assertions fail...

require 'test/unit'

class String
def to_tex
gsub( /(?!\w)'(.*?)'/, '`\1\'' )
end
end

class StringTC < Test::Unit::TestCase
def test_single_quotation
assert_equal( "`single quotation'", "'single quotation'".to_tex )
assert_equal( "isn't fool'd", "isn't fool'd".to_tex )
assert_equal( "2' x 4'", "2' x 4'".to_tex )
end
end

Any help would be appreciated and probably evoke "duh!"

Thanks,
--
Bil Kleb, Hampton, Vriginia
http://fun3d.lar...
4 Answers

ts

1/27/2005 10:21:00 AM

0

>>>>> "B" == Bil Kleb <Bil.Kleb@NASA.Gov> writes:

B> gsub( /(?!\w)'(.*?)'/, '`\1\'' )

(?!) is a zero-width negative look-ahead


B> assert_equal( "isn't fool'd", "isn't fool'd".to_tex )

When the regexp engine is at this position (just before ')

"isn't fool'd"

^
|

* it look if it don't have a word character (\w) : this is true because the
first next character is '

* then it look if it has ' : it's true and it match



Guy Decoux





Robert Klemme

1/27/2005 10:31:00 AM

0


"ts" <decoux@moulon.inra.fr> schrieb im Newsbeitrag
news:200501271020.j0RAKGL04736@moulon.inra.fr...
> >>>>> "B" == Bil Kleb <Bil.Kleb@NASA.Gov> writes:
>
> B> gsub( /(?!\w)'(.*?)'/, '`\1\'' )
>
> (?!) is a zero-width negative look-ahead
>
>
> B> assert_equal( "isn't fool'd", "isn't fool'd".to_tex )
>
> When the regexp engine is at this position (just before ')
>
> "isn't fool'd"
>
> ^
> |
>
> * it look if it don't have a word character (\w) : this is true because
the
> first next character is '
>
> * then it look if it has ' : it's true and it match

These seem to work:

>> class String
>> def to_tex
>> gsub( /(?:\A|(?![\w']))'(.*?)'/, '`\\1\'' )
>> end
>> end
=> nil
>> "'single quotation'".to_tex
=> "`single quotation'"

>> class String
>> def to_tex
>> gsub( /'((?:[^\\']|\\.)*)'/, '`\\1\'' )
>> end
>> end
=> nil
>> "'single quotation'".to_tex
=> "`single quotation'"

But I dunno whether they fix all of your problems.

Kind regards

robert

ts

1/27/2005 10:39:00 AM

0

>>>>> "R" == Robert Klemme <bob.news@gmx.net> writes:

R> But I dunno whether they fix all of your problems.

You want Onigurama, with a zero-width negative look-behind (?<!)


Guy Decoux






dblack

1/27/2005 11:54:00 AM

0