Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 22nd, 2012, 03:53 AM
Captain Planet's Avatar
Captain Planet Captain Planet is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 418 Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level)Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level)Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level) 
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.

Reply With Quote
  #2  
Old November 22nd, 2012, 08:27 AM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 48 m 54 sec
Reputation Power: 7053
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
How to program a basic, secure login system using PHP

Quote:
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

Reply With Quote
  #3  
Old November 22nd, 2012, 08:59 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,864 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 21 h 8 m 50 sec
Reputation Power: 813
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.

Reply With Quote
  #4  
Old November 22nd, 2012, 09:15 AM
Captain Planet's Avatar
Captain Planet Captain Planet is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 418 Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level)Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level)Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level) 
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.

Reply With Quote
  #5  
Old November 22nd, 2012, 09:21 AM
Captain Planet's Avatar
Captain Planet Captain Planet is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 418 Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level)Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level)Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level) 
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,")","\)")

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Regexes for left, right, contains and exact

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap