+ Reply to Thread
Results 1 to 26 of 26

Group string based on comma using regex

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Group string based on comma using regex

    Hello everyone

    Say I have string separeted by comma like
    Yasser, Ahmed, Reda, Khalil
    How can I group each part of those words within string using regex pattern?
    I am searching for something similar to (\,.*)(.*\,) but this is not right

    so I can isolate the strings using regex like that
    Yasser
    Ahmed
    Reda
    Khalil

    Thanks advanced for help
    Last edited by YasserKhalil; 03-20-2020 at 02:18 PM.
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,885

    Re: Group string based on comma using regex

    In your sample case "(\w+)" will do.

    Edit: Forgot to remove ?
    Last edited by CK76; 03-20-2020 at 02:35 PM.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Group string based on comma using regex


    Hi,

    why don't you use the easy Split VBA function ?

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    Thanks a lot for replies.
    That worked fine (\w+). Thanks a lot for solution.

    @Marc L
    I am just training some patterns on regex and I know Split is the best in this case.

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    Say A1 has the string Yasser, Ahmed, Reda, Khalil, Ahmed, Khalil
    How can using regex color the duplicate values within such pattern?

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,885

    Re: Group string based on comma using regex

    You can use some container to hold matches from pattern and then check for duplicate (dictionary, collection, two arrays etc).

    Ex: Using dictionary, add matches as Key. If .exists(string) in dictionary increment item by 1. Then use Key to find match in string and use position of string to format it.

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    I already tried but didn't succeed
    Please Login or Register  to view this content.
    Try the code with
    A1 = 2,4,6,8,12,14,18,23,35,78,101,38,30,205,2,101
    and you will see what I mean

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,885

    Re: Group string based on comma using regex

    See below sample.

    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    That's great. Thanks a lot
    As a matter of curiosity, is it possible to skip the first instances and highlight the rest?
    I mean like that
    01.png

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,885

    Re: Group string based on comma using regex

    Sure, that was the original intention when I used Redim y(1 to dic(Key))

    Modify 2nd loop to something like...
    Please Login or Register  to view this content.
    Edit: Woops, should have removed n in the n = calc. Code updated.
    Last edited by CK76; 03-20-2020 at 04:37 PM.

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    Thanks a lot for awesome help
    Best and Kind Regards

    ** I wish to be able to use regex for this purpose too(that will be open for other members) but for now this is solved.

  12. #12
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,885

    Re: Group string based on comma using regex

    You are welcome and thanks for the rep

    As for Regex, you only need to replace the logic to fill dictionary using Set matches = regex.Execute(string). Then looping match/submatch in the object.

    You'll probably find below link useful.
    https://analystcave.com/excel-regex-tutorial/

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Group string based on comma using regex

    Group string based on comma using regex
    Please Login or Register  to view this content.

  14. #14
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    Thanks a lot jindon for this masterpiece. I will try to study and get it well so as to learn new and we are learning the new from you all the time.

  15. #15
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    Can you please explain the pattern " *([^,]+)" and why this pattern (\w+) doesn't work with your code?

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Group string based on comma using regex

    Huhhh?

    Comma delimited string, so get the string other than comma.
    \w is not appropriate. This will only extract A-Za-z,space & 0-9, so if string contains hyphen/period/question mark, etc., it will not separate correctly.

    Why \w+ doesn't work? Because the pattern contains 2 parts.
    try change to " *(\w+)"

  17. #17
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    Thanks a lot .. so the secret in using space and asterisk before the pattern. Is it necessary in this case for any pattern that may be used?

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Group string based on comma using regex

    I don't know, but I think that's better, because who knows your actual string?
    It might contain comma and with/without space(s), so it will trim the spaces in
    Please Login or Register  to view this content.
    So that it will target to the trimmed word only.

  19. #19
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    Last point, I tried to skip the first instance in your code by editing this
    Please Login or Register  to view this content.
    But I am still get all the instances higlighted.

  20. #20
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    I could figure this point out but it is matter for me to give me your opinion
    Please Login or Register  to view this content.

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Group string based on comma using regex

    Code is not designed such, so you should try to build from a scratch for yourself.

    I will tell you that it will be not so simple...

  22. #22
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Group string based on comma using regex

    Please Login or Register  to view this content.

  23. #23
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    That's better certainly.
    Thank you very much for your pateince

  24. #24
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    Sorry for disturbing you again
    I have tested the code now with this string
    Please Login or Register  to view this content.
    Notice 2 is repeated three times so I intend to skip the first instance and highlight the others
    When running the code, I encountered an error "Invalid procedure call or argument" at this line
    Please Login or Register  to view this content.

  25. #25
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Group string based on comma using regex

    Please Login or Register  to view this content.

  26. #26
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Group string based on comma using regex

    Thank you very very much. That's awesome
    Best and Kind Regards

+ 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] Simplification for regex code to replace numbers in a comma delimited string
    By sintek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-08-2018, 06:30 AM
  2. Replies: 2
    Last Post: 10-18-2016, 12:05 AM
  3. Categorized Each String under Group (1 time), based on String content?
    By eryksd in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-17-2016, 08:02 PM
  4. [SOLVED] VBA string check without RegEx
    By VBA FTW in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-05-2013, 05:54 PM
  5. [SOLVED] Summing Values Based on a Comma Delimited String
    By bryan0 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-18-2013, 11:43 AM
  6. Use VBA and REGEX to Parse String
    By JohnM3 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-19-2011, 11:38 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