+ Reply to Thread
Results 1 to 8 of 8

Regex - Split string at fixed delimiter but keep "Groups" with same delimiter together

  1. #1
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Regex - Split string at fixed delimiter but keep "Groups" with same delimiter together

    In Range("A1") I have a string like One,Two,"Three,Four",Five,Six,"Seven,Eight",Nine,Ten,"Eleven,Twelve"

    The string has to be split at the comma but the Groups marked by the double quotes should stay intact.

    C1 to K1 should look like One Two "Three,Four" Five Six "Seven,Eight" Nine Ten "Eleven,Twelve"

    The double quotes should also be included in the final result.

    I have written a function that works well but I'm just curious if it's possible to do the same thing using Regex.

    Thanks
    Last edited by bakerman2; 08-26-2021 at 02:53 AM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  2. #2
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,477

    Re: Regex - Split string at fixed delimiter but keep "Groups" with same delimiter together

    I cannot answer your direct question because I do not program with Regular Expressions, and while I know you said you wrote your own function to do this, I thought you might like to see the function I wrote for this many, many, many years ago now.
    Please Login or Register  to view this content.
    The above function has four arguments... the first is the text that you want to split apart and is, of course, required; the second, the text you want to use as the delimiter is optional and is defaulted to a comma, but which can be any text string composed of one or more characters; the third is a Boolean which allows you to specify how consecutive delimiters are to be treated, where the default is False meaning the each delimited field will be returned even if that delimited field is the empty string ("") and where True will make the function treat consecutive delimiters as if they were a single delimiter; and the fourth is an optional built-in data type (VbCompareMethod - actually, a restricted Long underneath it all) which, if the delimiter has alphabetic characters in it, allows you to make the split case sensitive or not... the default is vbBinaryCompare (0) meaning the text must match the case of the delimiter you pass in exactly, set it to vbTextCompare (1) if you want the split to be performed with regard for the letter casing of the delimiter. Oh, and I guess I should mention... this function, like the Split function which it is modeled after, returns a zero-based one-dimensional String array.

    EDIT NOTE
    ===============================
    To use the above function with data on your worksheet, you should create a second function which allows you to specify the element to return. The following function does this...
    Please Login or Register  to view this content.
    To implement these two functions on your work sheet, put this normally entered formula into cell C1 and copy it across for as many cells as you think you will ever have output for, then copy all of those down to the bottom of your data (or beyond if you want to prepare for additional data being added at a later time)...
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    An interesting side note and something I was not aware of before... when you let Excel create an array for use in another function, whether it's another Excel function or a UDF (user defined function), that array appears to always be a one-based array. Even though my QuotedString function returns a zero-based array, the array Excel passes into the QS function is always one-based. So, if you use the QuotedString function in a formula, the first element's index value will always be one... when you call the QuotedSplit function directly from another VBA function or subroutine, the index value for the first element will always be zero.
    Last edited by Rick Rothstein; 08-26-2021 at 10:32 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,187

    Re: Regex - Split string at fixed delimiter but keep "Groups" with same delimiter together

    The below UDF when entered as an array function, it works as needed;

    Please Login or Register  to view this content.
    Attached Images Attached Images

  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Regex - Split string at fixed delimiter but keep "Groups" with same delimiter together

    Code for UDF
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  5. #5
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,222

    Re: Regex - Split string at fixed delimiter but keep "Groups" with same delimiter together

    Please try

    Please Login or Register  to view this content.
    C1
    =SpitQ($A1,COLUMNS($C1:C1))
    Attached Files Attached Files

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2506 Win 11
    Posts
    24,796

    Re: Regex - Split string at fixed delimiter but keep "Groups" with same delimiter together

    If you are looking for an alternative, there is Power Query

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: Regex - Split string at fixed delimiter but keep "Groups" with same delimiter together

    To all, I sincerely thank you for your contributions.

    @ Rick

    This one definitely goes into my 'Things to keep' folder.

    @ Haluk & Bo_Ry

    Great job, spot on. Both functions work as expected.

    This answers my initial question.

    @ kris

    We think a bit alike because this was the way I went with the function I wrote myself. Thanks

    @ Alan

    If I ever find the time to dive into Power Query I really should because this seems to be an awesome tool.

  8. #8
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Regex - Split string at fixed delimiter but keep "Groups" with same delimiter together

    Thanks for feed back and rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Add extra "InStr" function within a "Split" code solution
    By therealdees in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-18-2021, 08:20 AM
  2. [SOLVED] Function delimiter changed from "," to ";"
    By Franco403 in forum Excel General
    Replies: 5
    Last Post: 08-31-2015, 11:51 AM
  3. [SOLVED] How to split string based on "&" (VBA Code)
    By wolfdemon710 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-16-2015, 12:33 PM
  4. [SOLVED] Split String at every "/" then put split string results into a multiline textbox.
    By Andrew Andromeda in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-01-2015, 01:49 AM
  5. regex: include split "0=" like "a=" into .pattern
    By DavidRoger in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-02-2014, 01:32 AM
  6. Replies: 1
    Last Post: 08-15-2014, 06:00 AM
  7. [SOLVED] How do I split "A1B2" into "A1" and "B2" using text to column fun.
    By Jennifer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2005, 06:06 PM

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1