The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Regexes for left, right, contains and exact
Discuss Regexes for left, right, contains and exact in the Regex Programming forum on Dev Shed. Regexes for left, right, contains and exact Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 22nd, 2012, 03:53 AM
|
 |
Senior Member
|
|
Join Date: Aug 2003
Posts: 418
 
Time spent in forums: 2 Days 13 h 42 m 8 sec
Reputation Power: 10
|
|
|
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..
__________________
Captain Planet.
Last edited by Captain Planet : November 22nd, 2012 at 04:09 AM.
|

November 22nd, 2012, 08:27 AM
|
 |
Lost in code
|
|
|
|
|
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.
|

November 22nd, 2012, 08:59 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
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.
|

November 22nd, 2012, 09:15 AM
|
 |
Senior Member
|
|
Join Date: Aug 2003
Posts: 418
 
Time spent in forums: 2 Days 13 h 42 m 8 sec
Reputation Power: 10
|
|
Quote: | 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.
|

November 22nd, 2012, 09:21 AM
|
 |
Senior Member
|
|
Join Date: Aug 2003
Posts: 418
 
Time spent in forums: 2 Days 13 h 42 m 8 sec
Reputation Power: 10
|
|
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,")","\)")
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|