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.
Match a series of words
Moderator: General Moderators
- ridgerunner
- Forum Contributor
- Posts: 214
- Joined: Sun Jul 05, 2009 10:39 pm
- Location: SLC, UT
Re: Match a series of words
Easy. Try this:
Hope this helps!

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]);
}
?>-
bonekrusher
- Forum Newbie
- Posts: 2
- Joined: Fri Sep 17, 2010 9:48 am
Re: Match a series of words
Thanks for the help.
I ended up using the following:
This seems to work. Thoughts?
I ended up using the following:
Code: Select all
([(][A-Z]+[)]|[A-Z]+[/][A-Z]+)