#1
  1. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,470
    Rep Power
    653

    regexp to split string but exclude delimiters within parens


    I have a string of the form:
    Code:
    a,b,c,(d,e),f,g
    I want to split the string with ',' as the delimiter but if it is within (), I want that to be treated as a single element. In other words the resulting array should have 6 elements where the 4th element is "(d,e)". TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,896
    Rep Power
    9646
    If you can be sure that the only parentheses in the string are (a) balanced, (b) not nested, and (c) pairs whose contents you want to skip over, then you can search for a comma and follow it with a forward assertion to ensure there aren't any )s later in the string.
    Code:
    (?![^()]*\))
    But regular string methods would be more efficient.

IMN logo majestic logo threadwatch logo seochat tools logo