I want to allow anything between parantheses but not '-'

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

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

I want to allow anything between parantheses but not '-'

Post by raghavan20 »

I am trying to extract character between brackets...but I do not want text within brackets which has hyphen in it.

Code: Select all

<?php

$input = <<<EOD
Character data is roughly all the (non-markup) contents of XML documents, including whitespace between tags. 

Note that the XML parser does not add or remove any (whitespace), it is up to the application (you) to decide 

whether whitespace is significant. 
EOD;
echo preg_match_all("/\(([^-].*?)\)/mi", $input, $matches)."<br />";
print_r($matches);

?>
The non-markup is matched but it should not be...
output:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => (non-markup)
            [1] => (whitespace)
            [2] => (you)
        )

    [1] => Array
        (
            [0] => non-markup
            [1] => whitespace
            [2] => you
        )

)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

#\(([^-]*?)\)#
Post Reply