SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Reg expression question.

    What reg expression can be used to get text between ( and ).

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,461
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    Parentheses have "special meaning" in regex (they signify a "capture") so you need to escape them with a backslash so they are "literal" parentheses.
    Then depending on what you want to match and capture between them, you can either use a character class or the text strings in your expression.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I pretty much want to delete text between the Parentheses.

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A example of the reg expression would be nice lol, I still cant get mine to work.

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,461
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    So you want to keep the parentheses, and any non-text characters, removing only, and all, the text characters, if any are there?

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $s "This is some (stupid) text (poop).";
    echo 
    preg_replace("/\((.)+?\)/","(newtext)",$s); 

  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    The above regular expression might not be what you're after since it will have problems with nested sets of parentheses.

    The original post isn't very precise in saying what the expected inputs and outputs should be. Do you just want to 'get' the text between parentheses, or delete that text. Define 'text', is that anything at all?

    Anyway, here's my two pennies.

    Code:
    /
        \(              # Opening parenthesis
            (?>         # Start once-only sub-pattern
                [^()]+  # One or more non-parenthesis characters
                |       # Or
                (?R)    # Recursive match of the entire pattern (ie, nested parentheses)
            )*          # End once-only sub-pattern, match it zero or more times
        \)              # Closing parenthesis
    /x
    Of course, that is a bit of a mouthful! An example of its use would be:
    PHP Code:
    $text 'This is (not) a (quick (useful)) test.';
    echo 
    preg_replace('/\((?>[^()]+|(?R))*\)/'''$text); // This is  a  test. 
    If you want to keep the wrapping parentheses, just include them in the replacement.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •