Results 1 to 4 of 4

Thread: RegExp replace help

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    2

    RegExp replace help

    Hi

    I am fairly new to vb and have been trying to write some scripts to do some monotonous tasks at work. This is the scenario:-

    Every month we get a list of about 400 users from our HR department. This txt file is basically a list of profiles which need to be deleted for users that have left the company. Below is an example of the list

    \\severname\Profiles$\Username
    \\severname\Profiles$\Username
    \\severname\Profiles$\Username
    \\AN\ANDFS\HOMEDIRS4\C0202484\Profile
    \\AN\ANDFS\HOMEDIRS6\E1149536\Profile

    Now the lines with \\severname\Profiles$\Username are fine but what i need to do is change the lines that are like \\AN\ANDFS\HOMEDIRS6\E1149536\Profile so that they no longer have \Profile at the end. The HOMEDIRS dir and the folder after it change depending on the users ID and we have over 30,000 staff worldwide. I have done a bit of reading on RegExp and it seems the way to go but it totally confuses me. So please please any help would be appreciated.

    RB

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: RegExp replace help

    Once you've imported your text from the file, your regex pattern might look something like this:
    Code:
    ^(\\\\AN\\ANDFS\\HOMEDIRS[0-9]\\[0-9A-Z]{8})\\Profile$
    ^ signifies the beginning of a string, and $ signifies the end of it. The parentheses are for capturing a part of the tested string for usage later. The forward slash is a special escape character in regex, so to get a literal one, you need to double them up ("\\\\" is 2 literal forward slashes).

    Then you have your path info, where I'm making some assumptions about your data. "HOMEDIRS[0-9]" assumes that this will always look like "HOMEDIRS" followed by a single number, 0-9. "[0-9A-Z]{8}" assumes that this next part will always be a set of 8 alphanumeric, uppercase characters. Finally "Profile" at the end.

    So, to use it...
    Code:
    dim objRegExp
    set objRegExp = new RegExp
    with objRegExp
      .Pattern = "^(\\\\AN\\ANDFS\\HOMEDIRS[0-9]\\[0-9A-Z]{8})\\Profile$"
      .IgnoreCase = varIgnoreCase
      .Global = True
    end with
    ereg = objRegExp.replace("\\AN\ANDFS\HOMEDIRS6\E1149536\Profile","$1")
    set objRegExp = nothing
    msgBox ereg
    When ereg displays in msgBox, you should have "\\AN\ANDFS\HOMEDIRS6\E1149536". The $1 in the replace() method is a back reference to the part of the string you captured in the parentheses of your pattern.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    2

    Re: RegExp replace help

    That looks like exactly what I am after. Thanks very much. I'll let you know how I get on.

    regards

    RB

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: RegExp replace help

    Actually, one thing that should change...
    Code:
    .IgnoreCase = varIgnoreCase
    "varIgnoreCase" is a variable that got left out of the snippet I posted here. Change it to True or False, as per your preference.

Posting Permissions

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



Click Here to Expand Forum to Full Width