+ Reply to Thread
Results 1 to 12 of 12

Parse array of strings to compare/validate against specific format

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Parse array of strings to compare/validate against specific format

    hi,

    I have an array of strings. This string array is an array of file names. What i want to do is make sure that each file name has the following format.

    TextA_#_#



    If the file name does not then i want to store this filename into another array but i think i can do this part.

    What i am not clear on is the best way to "check" / "validate" that each entry in the array meets the requirement "Anytext_number_number"


    Actually, the more i think about it the more i would say each file name has to have
    1) 2 Underscores
    2) 3 elements parsed from the file name.
    the 3 elements can be a string or number.

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Parse array of strings to compare/validate against specific format

    It would be something similar to:

    Please Login or Register  to view this content.
    This of course on the fly and might need some tweaking. Let me know if it doesn't work.

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Parse array of strings to compare/validate against specific format

    i really like how compact and short this is........i was going a totally different way.......and its much longer.

    I did have one question that i need a little help with.

    if i have a text string to evaluate such as this: "textA_1234_5678_jkljl.xls"

    And i use the expression "*" + "_" + "*" + "_" + "*"

    to evaluate if my string is ok......it passes when it should fail.


    So maybe i cant use this approach..........

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Parse array of strings to compare/validate against specific format

    Here is what i came up with ...........i have not done it yet to handle the array yet.....just a single string....

    I think it handles all the cases i would need to consider to fail a string for not being the correct format..... "text_text_text.xls"

    Please Login or Register  to view this content.

  5. #5
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Parse array of strings to compare/validate against specific format

    Here is a worked example with the new variation.

    Please Login or Register  to view this content.
    If you have any other variations you can think of then it would probably be best to post a sample workbook with all the possible variations and what the end result would look like.

    Hope this helps.

    abousetta

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Parse array of strings to compare/validate against specific format

    thanks abousetta. I will check i out. I think yours might be better but i need to review it in more detail. Few things i am not sure whats going on just yet......THANKS!!!!!!!!

  7. #7
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Parse array of strings to compare/validate against specific format

    very neat......your code that is.........i new there had to be a more simple way to identify the number of "_" in a string than the way i did it.......thanks again.

  8. #8
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Parse array of strings to compare/validate against specific format

    I tested it out.....the only thing i found which i guess i need to fix is the code does not seem to handle this case correctly.

    aaaa__aaa.xls

    in the above case there are "2" underscores back-to-back. For some reason the code passes this. Not sure why just yet but i think it has to do with the "*" + "_" + "*" + "_" + "*"

  9. #9
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Parse array of strings to compare/validate against specific format

    as an option (based on Abousetta's code)
    Please Login or Register  to view this content.

  10. #10
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Parse array of strings to compare/validate against specific format

    Hi,

    I added this variant as part of the If statement:

    Please Login or Register  to view this content.
    I'm not a RegEx kinda guy but that seems to work also. I know that RegEx works with other languages and so if its going to be used for example on a website then you might to go down that approach. I just don't know the syntax well enough to help you there. In fact, I can barely put an IF and OR statement together in Excel... just got lucky this time I guess
    Last edited by abousetta; 04-19-2012 at 09:21 AM.

  11. #11
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Parse array of strings to compare/validate against specific format

    Thanks guys!

  12. #12
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Parse array of strings to compare/validate against specific format

    Just as a heads-up because I forget sometimes. If all the file names are coming from a worksheet, then converting the range to an array will always give you a 2D array and so you will need to check the elements like this:

    Please Login or Register  to view this content.
    instead of just

    Please Login or Register  to view this content.
    if they are in a column and the other way if they are all in a column.

    Good luck.

    abousetta

    P.S. Please don't forget to mark the thread as Solved when you are satisfied with the responses.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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