RegEx help (small)

B

Bil Kleb

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,
 
T

ts

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
 
R

Robert Klemme

ts said:
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
 
T

ts

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

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


Guy Decoux
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top