![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| ||||
| ||||
|
|
I'm baffled! I've been reading, for days, about how to create regular expressions.... and I'm lost. Can someone help me create RE's to do the following? 1) Test a "Username" string. The string can contain 2 to 15 word characters and hyphens but nothing else. I figured out /^\w{2,15}$/, but how to I tell it hyphens are cool as well? I tried /^-?\w{2,15}-?$/, which works if a hyphen starts or ends the string, but how can I tell it that hyphens are okay anywhere in the middle, and still limit the total string length to {2,15}? |
|
perl -e "$_=qq(5peter-john-l);print qq(MATCH) if (/^[\w-]{2,15}$/)" MATCH |
|
2) If it fails, I'm telling the user that the username is invalid, and am rejecting spaces embedded in the string (e.g., "user name" = false). However, I'd rather just trim whitespace if it falls at the start or end of the string before I test it, without bothering the user (e.g., trim(" username ") = "username", which would then test = true), but leave alone whitespace in the middle (e.g., " user name " = "user name", which would then test = false because of the space in the middle). Thanks for your help! Mike |
|
perldoc -q white that gives you |
#3
| |||
| |||
|
#4
| |||
| |||
|
#5
| |||
| |||
|
|
SORRY again! I think this will fix my first wrong post ===================================== I've seen a couple of web sites stating that: ---> The syntax of JavaScript RegEx's is the same as of Perl's So, a little rewriting of my previous answer will suffice: /*== JavaScript program ==*/ var myRE = /^[\w-]{2,15}$/; var myString = "5peter-johnny-l"; var theResult; if (myString.match(myRE)) { theResult = "MATCHED"; } else { theResult = "UNMATCHED"; } WScript.Echo(theResult); /* WARNING! This line is Microsoft's JScript "standard output" - just delete it */ /*== End of JavaScript program ==*/ |
![]() |
| Thread Tools | |
| Display Modes | |
| |