Raymond R Raymond R - 1 year ago 74
Ruby Question

Regex to include single but exclude double hyphen

I'm trying to get a regex where words with single hyphens are counted as one, but those double hyphens are counted as two. Currently, this is what I have:


What do I need to change to make this work?

To clarify, I'm using this regex to count words.

single-dash (1 word)
double--dash (2 words)

I have tried to include a negative lookahead as suggested, but now it's igoring all words before the double dash (link)

Answer Source

Assuming English alphabet, and that there can only be at most one symbol (any of [’'-]) between substring of alphanumeric characters, and that there can only be at most one of those symbol at the beginning and the end of a "word" ("word" as defined in the question).


Test cases:

"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 
the fo’c’s’le’s timbers
Three-hundred-year-old trees are an indeterminate number of trees that are each aged 300 years.

Demo at Rubular

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download