Page 1 of 1

Match a series of words

Posted: Fri Sep 17, 2010 9:58 am
by bonekrusher
Hi, I am trying to figure out how to match a series of words that meet the following criteria.

Input String: This is the end (THIS) of the song N/A

I want to match any word in Parentheses or any word that has a slash in it (e.g AC/DC)

So with this input, I want to match (THIS) and N/A

Thanks for the help.

Re: Match a series of words

Posted: Fri Sep 17, 2010 6:59 pm
by ridgerunner
Easy. Try this:

Code: Select all

<?php // test.php 2010-09-17
$text = 'This is the end (THIS) of the song N/A';
preg_match_all('%# Match a word as either a "(WORD)" or a "WORD/WORD".
    (?<=\s|^)               # Word preceded by whitespace or start of string.
    (?:                     # Begin group of two "word" alternatives...
      \(\S+\)               # Match either a "(WORD)" in parentheses...
    | [\w\-]+(?:/[\w\-]+)+  # or a "WORD/WORD" containing forward slash(es).
    )                       # End group of two alternatives.
    (?=[\s.,;:!?]|$)        # Word followed by punctuation, whitespace or EOS.
    %x', $text, $matches, PREG_PATTERN_ORDER);
sprintf("There were %d matches\n", count($matches[0]));
for ($i = 0; $i < count($matches[0]); $i++) {
	printf("Match %d = \"%s\"\n", $i + 1, $matches[0][$i]);
}
?>
Hope this helps!
:)

Re: Match a series of words

Posted: Mon Sep 20, 2010 10:40 am
by bonekrusher
Thanks for the help.

I ended up using the following:

Code: Select all

([(][A-Z]+[)]|[A-Z]+[/][A-Z]+)
This seems to work. Thoughts?