Match a series of words

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
bonekrusher
Forum Newbie
Posts: 2
Joined: Fri Sep 17, 2010 9:48 am

Match a series of words

Post 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.
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Match a series of words

Post 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!
:)
bonekrusher
Forum Newbie
Posts: 2
Joined: Fri Sep 17, 2010 9:48 am

Re: Match a series of words

Post 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?
Post Reply