Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regular expression for strings and negative number inputs

Posted on 2010-09-20
9
Medium Priority
?
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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

722 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