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 March 13th, 2009, 09:39 AM
vayumahesh vayumahesh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 31 vayumahesh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 36 m 52 sec
Reputation Power: 9
String Pattern

I need a regular expression to validate a string with the following requirements.

1. May contain Upper case, Lower case alphabets, Numbers and hyphens ( this works - "^[a-zA-Z0-9-]*$" )

Not sure how to handle the second requirement

2. The hyphen should not be at the beginning or end of the string. Also, no more than one hyphen consecutively.

Example Strings

B, 5, 12Ab3, Ba3, aBc-32, a1-b2-c3

Reply With Quote
  #2  
Old March 13th, 2009, 03:18 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,689 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 3 h 28 m 41 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Quote:
Originally Posted by vayumahesh
2. The hyphen should not be at the beginning or end of the string. Also, no more than one hyphen consecutively.

Reword that and break it down a bit:
  1. First character must be a letter or number
  2. Last character must be a letter or number
  3. Each hyphen must not have a hyphen on either side
So basically, letters and numbers are all treated the same, but hyphens have to be handled differently.
If you use assertions (lookbehinds and lookaheads) it's quite simple:
Code:
/^([a-zA-Z0-9]+|(?<!^|-)-(?!-|$))*$/

(?<!^|-) checks that the hyphen doesn't have a start-of-string or hyphen before,
(?!-|$) checks that it doesn't have a hyphen or end-of-string after.



Alternative ending:
Code:
function validate(string)
    if (string has "--" OR string starts with "-" OR string ends with "-") return FALSE
    return whether string passes /^[a-zA-Z0-9-]*$/
}

This may actually be faster, depending on the language.

Last edited by requinix : March 13th, 2009 at 04:41 PM.

Reply With Quote
  #3  
Old March 13th, 2009, 04:35 PM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
Quote:
Originally Posted by requinix
...

Alternative ending:
Code:
function validate(string)
    if (string has "--" OR string starts with "-" OR string ends with "-") return FALSE
    return whether string passes /^[a-zA-Z0-9-]*$/
}

This may actually be faster, depending on the language.


@OP:
Nixie has a good point about the "Alternative ending". Although I don't think the speed will matter much unless you will be going to perform these kind of validations hundreds of times a second or if you're validating strings of thousands of characters long.
The real advantage of the "Alternative ending" is the fact that it's so much easier to read/understand and thus easier to maintain. The readability of code is also worth a lot IMO!

Also note that because of the *, that pattern will also match an empty string.

Last edited by prometheuzz : March 13th, 2009 at 05:08 PM.

Reply With Quote
  #4  
Old March 16th, 2009, 07:58 AM
vayumahesh vayumahesh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 31 vayumahesh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 36 m 52 sec
Reputation Power: 9
Thanks for your replies. But the regular expression suggested by requinix does not work. I am getting "Syntax error in Regular Expression" error. Can you please verify.

Reply With Quote
  #5  
Old March 16th, 2009, 04:48 PM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
Quote:
Originally Posted by vayumahesh
Thanks for your replies. But the regular expression suggested by requinix does not work. I am getting "Syntax error in Regular Expression" error. Can you please verify.


I works all right.
Can yo post how you used it and with what compiler/interpreter (what language did you use)?

Reply With Quote
  #6  
Old March 17th, 2009, 07:47 AM
vayumahesh vayumahesh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 31 vayumahesh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 36 m 52 sec
Reputation Power: 9
I am testing on a HTML file in IE 7. Here is my HTML file.

<html>
<head>

<script type="text/javascript">
<!--
String.prototype.isText = function () {return /^([a-zA-Z0-9]+|(?<!^|-)-(?!-|$))*$/.test(this)}

function checkForText () {if (!this.value.isText()) {
alert ('Please, check the format for ' + this.previousSibling.data.replace(/\s+$/, '') + '.');
this.value = '';
this.focus();
}}

if (document.getElementById) onload = function () {
var e, i;
for (i = 0; e = document.forms[0].elements[i]; i++) {if (e.type == 'text') e.onchange = checkForText}
}
// -->
</script>
</head>

<body>

<form action="test.html">
<fieldset>
<legend>Regular Expression Test Form</legend>
<label>Test String1 <input name="test1" type="text"></label>
<button type="submit">Submit</button>
</fieldset>
</form>

</body>
</html>

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > String Pattern

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