RegEx help (small)

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.larc.nasa.gov

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

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

   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

"ts" <decoux@moulon.inra.fr> schrieb im Newsbeitrag
news:200501271020.j0RAKGL04736@moulon.inra.fr...

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

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

> 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

But I dunno whether they fix all of your problems.

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

Guy Decoux

Hi --

···

On Thu, 27 Jan 2005, Robert Klemme wrote:

These seem to work:

   gsub( /(?:\A|(?![\w']))'(.*?)'/, '`\\1\'' )
   gsub( /'((?:[^\\']|\\.)*)'/, '`\\1\'' )

Also:

    gsub( /(^|\W)'(.*?)'/, '\1`\2\'' )

passed the tests.

David

--
David A. Black
dblack@wobblini.net