Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regular expression for strings and negative number inputs

Posted on 2010-09-20
9
Medium Priority
?
292 Views
Last Modified: 2012-06-27
Greetings, I'm having trouble nailing down the two regular expressions which successfully meet the following conditions:

The first regular expression should accept an alphanumeric string of length 255, starting with a non-numeric or special character.  Or, only -1.  Or only -2.
I've tried, unsuccessfully:  ^[a-zA-Z0-9]*|-1|-2$ as -12 is accepted
Valid inputs:
Some text here
Hi 45 my name is
-1
-2

Invalid Inputs
-1is my name
1is my name
888777
-11
-3

The second regular expression should accept a positive numeric sequence of length 4.  Or, only -1.  Or only -2.
I've tried ^[0-9]{4}|-1|-2$ but it seems -12 is accepted.
Valid inputs:
0000
9999
-1
-2

Invalid Inputs
99999
88
-11
-3

Thanks in advance for your assistance.
0
Comment
Question by:allenanderson
  • 4
  • 3
  • 2
9 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33718082
for second

[0-9]{4}|\-[1-2]


0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33718122
for first

\D{1}[a-zA-Z0-9]{1-254}|\-[1-2]

but i have a confusion, wont '-1is my name' be the valid input since you said
<<The first regular expression should accept an alphanumeric string of length 255, starting with a non-numeric or special character>>

Also i am not sure, if 255 is the max length or only length to be accepted
0
 

Author Comment

by:allenanderson
ID: 33718260
gurvinder372,
Thanks for the quick reply.  I made a typo.  For the first regular expression, it should start with a non-numeric and non-special character.  So only lower or upper case letter.
Thanks again!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33718284
then try

[a-zA-Z]{1}[a-zA-Z0-9]{1-254}|\-[1-2]

0
 

Author Comment

by:allenanderson
ID: 33718510
gurvinder372.  Thanks again for your help.  Quick follow up.
[0-9]{4}|\-[1-2]:  1245a6 is successful as 1245 matches the [0-9]{4}.  This should fail.   Any thoughts on how, through the regular expression, to not allow input with length more than 4.
[a-zA-Z]{1}[a-zA-Z0-9]{1-254}|\-[1-2] seems to require 254 characters?  Can i get around this by just using * ?  Or, any other thoughts how to range the length between 0 and 255.
Thanks again.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33718737

Regex 1:

^(?:-[12]|[a-zA-Z][a-zA-Z0-9 ]{0,254})$

Regex 2:

^(?:-[12]|\d{4})$

Open in new window

0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 600 total points
ID: 33718743
which platform are you using? java or javascript?

give the starting character and end char

^[0-9]{4}|\-[1-2]$

and

[a-zA-Z]{1}[a-zA-Z0-9]{1-254}|\-[1-2]
should work for characters length 2-255 since i have given {1,254} which means anywhere from 1 to 254
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1400 total points
ID: 33718760
Correction:
Regex 1:

^(?:-[12]|(?:[a-zA-Z][a-zA-Z0-9 ]{0,254})?)$

Regex 2:

^(?:-[12]|\d{4})$

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33724934
NP. Glad to help  :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question