Thread: 'net Regex Help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    'net Regex Help


    Hi folks , Im new to regex and was hoping someone might be able to give me some advice.

    Im trying to use regex in VB.Net to split a string whenever there is a space except when it between quotes. I also want to keep the quotes.

    I tried using \s+(?=([^""]*""[^""]*""[^""]*)*$|[^""]*$)

    and it works on most string but here are a few lines that it doesnt work on !

    /config/userctrl/A/enc "X001" "X101" "X201" "X300"

    I want to get

    /config/userctrl/A/enc
    "X001"
    "X101"
    "X201"
    "X300"

    But get

    /config/userctrl/A/enc
    "X300"
    "X001"
    "X300"
    "X101"
    "X300"
    "X201"
    "X300"
    "X300"

    Same thing happens with

    /config/userctrl/A/btn "O40" "O42" "X201" "X300" "P0051" "P0052" "P0053" "P0058"

    and /ch/01/config "Drum Room" 2 CY 1
    gives me
    /ch/01/config
    "Drum Room" 2 CY 1
    "Drum Room"
    2
    CY
    1

    Where am I going wrong ???
    Thanks in advance for any help you can give

    Kev
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    Actually the expression is correct: it's your output that's wrong.

    The parentheses you used meant that the regex is capturing whatever was matched.
    Code:
    /config/userctrl/A/enc ' segment
    "X300"                 ' capture
    "X001"                 ' segment
    "X300"                 ' capture
    "X101"                 ' segment
    "X300"                 ' capture
    "X201"                 ' segment
    "X300"                 ' capture
    "X300"                 ' segment
    There may be an option in .NET where you can exclude the captured groups and keep only the actual split segments themselves, but you can just as easily make that group not capture.
    Code:
    "\s+(?=(?:[^""]*""[^""]*""[^""]*)*$|[^""]*$)"
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by requinix
    Actually the expression is correct: it's your output that's wrong.

    The parentheses you used meant that the regex is capturing whatever was matched.
    Code:
    /config/userctrl/A/enc ' segment
    "X300"                 ' capture
    "X001"                 ' segment
    "X300"                 ' capture
    "X101"                 ' segment
    "X300"                 ' capture
    "X201"                 ' segment
    "X300"                 ' capture
    "X300"                 ' segment
    There may be an option in .NET where you can exclude the captured groups and keep only the actual split segments themselves, but you can just as easily make that group not capture.
    Code:
    "\s+(?=(?:[^""]*""[^""]*""[^""]*)*$|[^""]*$)"
    Worked perfectly

    Thank you for your help.

    Kev

IMN logo majestic logo threadwatch logo seochat tools logo