+ Reply to Thread
Results 1 to 13 of 13

RegEx with unique matches

  1. #1
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    RegEx with unique matches

    Hi all,

    Which pattern would I have to use to get all unique fiscal years in the following string:

    Please Login or Register  to view this content.
    Currently I use

    Please Login or Register  to view this content.
    The desired result should be

    Please Login or Register  to view this content.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,717

    Re: RegEx with unique matches

    You have already done the heavy lifting. (The parentheses around "FY.." are not necessary, but they don't hurt either.) To view your results:
    Please Login or Register  to view this content.
    This will print the desired results to the Immediate window as you have shown in your post. Where do you want your desired results to actually appear?
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,149

    Re: RegEx with unique matches

    You can try this;

    Please Login or Register  to view this content.
    Last edited by Haluk; 06-26-2020 at 12:41 PM.

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

    Re: RegEx with unique matches

    Do you really need to use Regular Expressions for this? The following code creates a Matches array (zero-based) that contains the unique FY's. The output for it shows in the Immediate Window.
    Please Login or Register  to view this content.
    NOTE: I noted you used Str as a variable name (I changed it to S in the above code). Str is the name of a built-in text function and, while VBA will let you do it, it is not a good idea to use function names for your variable names.
    Last edited by Rick Rothstein; 06-26-2020 at 01:38 PM.

  5. #5
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: RegEx with unique matches

    One more option without RegEx since you have office 365 using "Unique" function

    Please Login or Register  to view this content.
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

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

    Re: RegEx with unique matches

    Yes, that probably will work for the OP as he has the same version of Excel as you do; however, it does not work for me with my version of Excel.

  7. #7
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: RegEx with unique matches

    @Rick Yes, it wouldn't work on older versions that's why I specifically stated office 365 for the OP ... Perhaps it's time to upgrade & explore the new options

    By the way, congrats on crossing the 1200 posts & getting Forum Expert title which you definitely deserve

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

    Re: RegEx with unique matches

    Quote Originally Posted by nankw83 View Post
    By the way, congrats on crossing the 1200 posts & getting Forum Expert title which you definitely deserve
    Thank you for the congrats and kind words... much appreciated. To tell you the truth, though, I am kind of surprise you noticed that.

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

    Re: RegEx with unique matches

    RegEx with unique matches
    Please Login or Register  to view this content.

  10. #10
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,149

    Re: RegEx with unique matches

    Another alternative to get a unique list, is using the "Dictionary" object. (In this case, we don't have to deal with On Error... statement)

    Please Login or Register  to view this content.
    Last edited by Haluk; 06-27-2020 at 05:00 AM.

  11. #11
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: RegEx with unique matches

    Thanks guys!! All very helpful and getting the results I need. However, I was wondering if there is a way to get the same output by using a "negative lookahead" inside the RegEx pattern instead? Is that feasible? I'm new to RegEx hence trying to understand how powerful it is.

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

    Re: RegEx with unique matches

    No in vba regexp.

  13. #13
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: RegEx with unique matches

    Good to know. Thanks!

+ 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] Extract matches of all the groups within regex pattern
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-10-2021, 10:35 AM
  2. Unique Combinations with Matches
    By Dshorrosh in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 07-29-2020, 10:27 AM
  3. [SOLVED] Returning ALL Unique Matches
    By Footnah in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-06-2019, 08:37 PM
  4. [SOLVED] Instr with RegEx - Runtime error 5017 (RegEx Code provided)
    By dluhut in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-28-2018, 05:02 PM
  5. [SOLVED] Generate unique numbers according to specific regex pattern
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 28
    Last Post: 08-14-2018, 04:16 PM
  6. [SOLVED] Pattern for regex that matches one digit at least
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-10-2018, 01:57 AM
  7. Formula to return unique matches
    By davegugg in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-13-2011, 05:20 PM

Tags for this Thread

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