Solved

Regular expression for strings and negative number inputs

Posted on 2010-09-20
9
285 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:gurvinder372
ID: 33718082
for second

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


0
 
LVL 40

Expert Comment

by:gurvinder372
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 40

Expert Comment

by:gurvinder372
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:gurvinder372
gurvinder372 earned 150 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 350 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regex in powershell (part 2) 6 85
Python negative lookahead for aaa:bbb@hostname 6 112
regex code to filter this ip's? 2 37
Date input validation (Regular Expressions) [2] 4 59
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

810 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