jonagoldman
jonagoldman

Reputation: 8754

Help with this reg. exp. in PHP

i don't know about regular expressions, I asked here for one that: gets either anything up to the first parenthesis/colon or the first word inside the first parenthesis. This was the answer:

preg_match('/(?:^[^(:]+|(?<=^\\()[^\\s)]+)/', $var, $match);

I need an improvement, I need to get either anything up to the first parenthesis/colon/quotation marks or the first word inside the first parenthesis.

So if I have something like:

$var = 'story "The Town in Hell"s Backyard';     // I get this: $match = 'story';
$var = "screenplay (based on)";             // I get this: $match = 'screenplay';
$var = "(play)";                                  // I get this: $match = 'play';
$var = "original screen";              // I get this: $match = 'original screen';

Thanks!

Upvotes: 0

Views: 90

Answers (1)

swestrup
swestrup

Reputation: 4209

Its a simple change:

preg_match('/(?:^[^(:"]+|(?<=^\()[^\s)]+)/', $var, $match);

I just added quotes inside the [^...] box, along with the colon and open parens.

Upvotes: 1

Related Questions