#1
  1. Senior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    421
    Rep Power
    12

    Regexes for left, right, contains and exact


    Hi, folks.

    I'm looking for regular expressions which perform the following string functions:

    left
    right
    contains
    exact

    I'm actually using VBScript, but the reason I can't use the built-in functions is because the user will be selecting the 'search type' on-the-fly. As such, I'll be generating thr RegExp pattern on-the-fly too.

    An example string which I'm searching through could contain ANY character. An example might be:

    HKLM\SYSTEM\CurrentControlSet\services\whatever

    I'm not sure if the backslashes add complexity to the regexp? I was hoping there is a wildcard character to help me perform these?

    At the moment, I've got the following....

    'starts
    'regexValid.Pattern = "^test"

    'ends
    'regexValid.Pattern = "test$"

    'contains
    'regexValid.Pattern = "test?"

    'exact
    regexValid.Pattern = "^test$"

    Do these look ok? Of is there a fundamental regex aspect which I'm overlooking? Thanks..
    Last edited by Captain Planet; November 22nd, 2012 at 05:09 AM.
    Captain Planet.
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    The start and end ones are correct. The contains one should not have a ? in it, that means "tes" optionally followed by a t. The exact match one will work too, but it's a lot more efficient to just use an equals comparison.

    Remember that you'll have to escape all special regular expression characters that appear in the user input. Some languages have a utility function that does this, I'm not sure if vbscript does.

    In almost all programming languages, using string comparison functions for these four types of matches is a lot more efficient that regexes.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    I don't even see why constructing different regexes is better than calling different functions. I mean, you'll have to go through each case, anyway.
  6. #4
  7. Senior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    421
    Rep Power
    12
    Originally Posted by E-Oreo
    Remember that you'll have to escape all special regular expression characters that appear in the user input. Some languages have a utility function that does this, I'm not sure if vbscript does.
    Thanks folks. Funny you mention the above, as I tested with:

    C:\

    and the backslash completely screwed it up. I replaced the "\" with a "\\" and it worked.

    Hmmm. It'd be nice to treat a straing of text as a literal string (say, enclose it in brackets) or something similar.....so it doesnt parse things like "\" as a regexp symbol.
    Captain Planet.
  8. #5
  9. Senior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    421
    Rep Power
    12
    Using the following now for a test:

    Code:
    		CSSelectData = replace(CSSelectData,"\","\\")
    		CSSelectData = replace(CSSelectData,"[","\[")
    		CSSelectData = replace(CSSelectData,"^","\^")
    		CSSelectData = replace(CSSelectData,"$","\$")
    		CSSelectData = replace(CSSelectData,".","\.")
    		CSSelectData = replace(CSSelectData,"|","\|")
    		CSSelectData = replace(CSSelectData,"?","\?")
    		CSSelectData = replace(CSSelectData,"*","\*")
    		CSSelectData = replace(CSSelectData,"+","\+")
    		CSSelectData = replace(CSSelectData,"(","\(")
    		CSSelectData = replace(CSSelectData,")","\)")
    Captain Planet.

IMN logo majestic logo threadwatch logo seochat tools logo