, 목적은 두 개의 연속 hypens를 포함하는 두 개의 단어로 간주되어야하는 단어를 계산하는 것입니다. 단일 정규식에서 모든 것을 시도하는 대신 두 개 이상의 연속 된 하이픈의 모든 인스턴스를 공백으로 대체하여 단어를 두 단어로 분리 한 다음 단어를 계산했습니다.
def count_words(str)
str.gsub(/-{2,}/, ' ').scan(/[a-zA-Z0-9’'-]+/).size
end
데모 용으로 @nhahtdh 테스트 문자열의 일부를 사용합니다.
#=> "\"Us and Them\"’s inclusion on the album The Dark Side of the Moon\nYou Am I’s latest CD\nThe 69’ers’ drummer, Tom Callaghan (only the second apostrophe is possessive)\nHis ’n’ Hers’ first track is called \"Joyriders\".[18]\nWas She's success greater, or King Solomon’s Mines's?\nRock 'n' Roll\n’bout for about, ’less for unless, ’twas for it was\n’70s for 1970s\n"
count_words(str) #=> 63
@nhahtdh 이상 str
대해 동일한 개수 (63)를 얻을 @mudasobwa
str =<<BITTER_END
"Us and Them"’s inclusion on the album The Dark Side of the Moon
You Am I’s latest CD
The 69’ers’ drummer, Tom Callaghan (only the second apostrophe is possessive)
His ’n’ Hers’ first track is called "Joyriders".[18]
Was She's success greater, or King Solomon’s Mines's?
Rock 'n' Roll
’bout for about, ’less for unless, ’twas for it was
’70s for 1970s
BITTER_END
.
http://stackoverflow.com/questions/12384555/regex-not-to-allow-double-underscores는 속임수 일 수는 있지만 너무 많지는 않습니다. – Terminus
"2로 계산"이란 무엇을 의미합니까? "순진한"단어입니까? "наивный"는 한 마디입니까? – mudasobwa
나는 그것을 더 제한적인 정규 표현식으로 재 작성하려고한다 :'[ '']? \ b [a-zA-Z0-9] + ([ ''-] [a-zA-Z0-9] +) * \ b 그것이 당신이 원하는 것인지 확신 할 수는 없지만. – nhahtdh