Consulting

Results 1 to 9 of 9

Thread: Solved: Regex Query

  1. #1

    Solved: Regex Query

    Hello all,

    I am trying to filter through files of a specific format and have a regex query of [A-Za-z][0-9][0-9][0-9][0-9][0-9][0-9]

    Unfortunately I am also picking up files with this name abc123456def but I only want c123456.

    I am assuming that it is doing this because c123456 is in the middle of abc123456def...

    How do I limit this to only pick up the exact regex expression?

    Any help is appreciated.

    Thanks

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Have you tried: [Cc][0-9][0-9][0-9][0-9][0-9][0-9] or [C-Cc-c][0-9][0-9][0-9][0-9][0-9][0-9]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Quote Originally Posted by Simon Lloyd
    Have you tried: [Cc][0-9][0-9][0-9][0-9][0-9][0-9] or [C-Cc-c][0-9][0-9][0-9][0-9][0-9][0-9]
    Thanks Simon but unfortunately it did not work!!

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Maybe its: [.C][0-9][0-9][0-9][0-9][0-9][0-9]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Look here half way down!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  6. #6
    VBAX Contributor
    Joined
    Aug 2006
    Location
    Hampshire, UK
    Posts
    140
    Location
    Hi

    $ anchors the regex at the start of the string expression being tested, so your pattern should be:

    $[A-Za-z]\d{6}

    Richard

  7. #7
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Richard i know nothing about regex as you can tell but could you explain why your suggestion will only pick out the C's and why {6} will find digits 0-9 (although i understand that it probably means 6 characters after the letter(s))?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    KB Item on RegExp
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Contributor
    Joined
    Aug 2006
    Location
    Hampshire, UK
    Posts
    140
    Location
    Quote Originally Posted by Simon Lloyd
    Richard i know nothing about regex as you can tell but could you explain why your suggestion will only pick out the C's and why {6} will find digits 0-9 (although i understand that it probably means 6 characters after the letter(s))?
    Hi Simon

    It won't just pick out the Cs - it matches a pattern at the start of the string under test that begins with an alphabet character (upper or lower case doesn't matter) followed by a string of numeric digits 6 characters long (as you rightly identified).

    Hence:

    A000001.xls

    Z123456RichardFile.txt

    Q65432199999.xls

    will all result in a match, but:

    123456tyu.xls

    ABC123456.xls

    won't as they don't meet the specified pattern.

    \d is a shortcut expression for [0-9]. The {6} can be used to specify a given number of characters and, additionally a minimum number of characters:

    \d{2,}

    a maximum number of characters:

    \d{,3}

    or a specified range of characters:

    \d{2,3}

    (which is 'between 2 and 3 numeric characters').

    Richard

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •