$re = '/[a-zA-Z0-9]+(?:\'[a-zA-Z0-9])*(?:-[a-zA-Z0-9]+)*/';
$str = 'October arrived, spreading a damp chill over the grounds and into the castle. Madam Pomfrey, the nurse, was kept busy by a sudden spate of colds among the staff and students. Her Pepperup potion worked instantly, though it left the drinker smoking at the ears for several hours afterward. Ginny Weasley, who had been looking pale, was bullied into taking some by Percy. The steam pouring from under her vivid hair gave the impression that her whole head was on fire.
Raindrops the size of bullets thundered on the castle windows for days on end; the lake rose, the flower beds turned into muddy streams, and Hagrid\'s pumpkins swelled to the size of garden sheds. Oliver Wood\'s enthusiasm for regular training sessions, however, was not dampened, which was why Harry was to be found, late one stormy Saturday afternoon a few days before Halloween, returning to Gryffindor Tower, drenched to the skin and splattered with mud.
Even aside from the rain and wind it hadn\'t been a happy practice session. Fred and George, who had been spying on the Slytherin team, had seen for themselves the speed of those new Nimbus Two Thousand and Ones. They reported that the Slytherin team was no more than seven greenish blurs, shooting through the air like missiles.
As Harry squelched along the deserted corridor he came across somebody who looked just as preoccupied as he was. Nearly Headless Nick, the ghost of Gryffindor Tower, was staring morosely out of a window, muttering under his breath, ". . . don\'t fulfill their requirements . . . half an inch, if that . . ."
"Hello, Nick," said Harry.
"Hello, hello," said Nearly Headless Nick, starting and looking round. He wore a dashing, plumed hat on his long curly hair, and a tunic with a ruff, which concealed the fact that his neck was almost completely severed. He was pale as smoke, and Harry could see right through him to the dark sky and torrential rain outside.
"You look troubled, young Potter," said Nick, folding a transparent letter as he spoke and tucking it inside his doublet.
"So do you," said Harry.
"Ah," Nearly Headless Nick waved an elegant hand, "a matter of no importance. . . . It\'s not as though I really wanted to join. . . . Thought I\'d apply, but apparently I \'don\'t fulfill requirements\' -"
In spite of his airy tone, there was a look of great bitterness on his face.
"But you would think, wouldn\'t you," he erupted suddenly, pulling the letter back out of his pocket, "that getting hit forty-five times in the neck with a blunt axe would qualify you to join the Headless Hunt?"
"Oh - yes," said Harry, who was obviously supposed to agree.
"I mean, nobody wishes more than I do that it had all been quick and clean, and my head had come off properly, I mean, it would have saved me a great deal of pain and ridicule. However -" Nearly Headless Nick shook his letter open and read furiously: "\'We can only accept huntsmen whose heads have parted company with their bodies. You will appreciate that it would be impossible otherwise for members to participate in hunt activities such as Horseback Head-Juggling and Head Polo. It is with the greatest regret, therefore, that I must inform you that you do not fulfill our requirements. With very best wishes, Sir Patrick Delaney-Podmore.\'"
Fuming, Nearly Headless Nick stuffed the letter away.
"Half an inch of skin and sinew holding my neck on, Harry! Most people would think that\'s good and beheaded, but oh, no, it\'s not enough for Sir Properly Decapitated-Podmore."
Nearly Headless Nick took several deep breaths and then said, in a far calmer tone, "So - what\'s bothering you? Anything I can do?"
"No," said Harry. "Not unless you know where we can get seven free Nimbus Two Thousand and Ones for our match against Sly -" ';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php